sqlite解析字符串日期至日期类型
我正在尝试将格式的字符串日期转换/解析:mm/dd/yyyy或m/d/yyyy在列中的日期(“ yyyy-mm-dd”)。
Create table date_test ( string_date TEXT );
insert into date_test values ('5/17/1967'), ('3/7/1936'), ('10/20/1930');
select string_date, "function to parse the string" as date_as_date from date_test;
-- Expected Result:
--------------------------------------
--string_date date_as_date
--------------------------------------
--5/17/1967 1967-05-17
--3/7/1936 1936-03-07
--10/20/1930 1930-10-20
-- I was trying to parse the date, but it gets a bit out of hand with the year:
SELECT substr('5/17/1967', 0, INSTR('5/17/1967', '/')) as Month;
SELECT substr(substr('5/17/1967', INSTR('5/17/1967', '/')+1), 0, INSTR(substr('5/17/1967', INSTR('5/17/1967', '/')+1), '/')) as Day;
我很想使用Regex,但是Sqlite不支持它。有建议吗?
I'm trying to convert/parse a string date of a format: MM/DD/YYYY or M/D/YYYY to a DATE("YYYY-MM-DD") in a column.
Create table date_test ( string_date TEXT );
insert into date_test values ('5/17/1967'), ('3/7/1936'), ('10/20/1930');
select string_date, "function to parse the string" as date_as_date from date_test;
-- Expected Result:
--------------------------------------
--string_date date_as_date
--------------------------------------
--5/17/1967 1967-05-17
--3/7/1936 1936-03-07
--10/20/1930 1930-10-20
-- I was trying to parse the date, but it gets a bit out of hand with the year:
SELECT substr('5/17/1967', 0, INSTR('5/17/1967', '/')) as Month;
SELECT substr(substr('5/17/1967', INSTR('5/17/1967', '/')+1), 0, INSTR(substr('5/17/1967', INSTR('5/17/1967', '/')+1), '/')) as Day;
I would love to use Regex, but SQLite doesn't support it. Any suggestion?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我找到了一种方法,也许有点令人费解,但它在我的数据集上工作,在sqlite上
创建表并插入一些测试数据:
转换字符串日期执行日期数据类型的查询(假设该年度始终是4位数字):
I found a way, maybe a bit convoluted but it works on my dataset on SQLite
Create the table and insert some test data:
The query that transform the string date do the date data type (assuming that the year is always 4 digits):