sqlite解析字符串日期至日期类型

发布于 2025-02-01 03:58:40 字数 823 浏览 1 评论 0原文

我正在尝试将格式的字符串日期转换/解析: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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

阳光的暖冬 2025-02-08 03:58:40

我找到了一种方法,也许有点令人费解,但它在我的数据集上工作,在sqlite上

创建表并插入一些测试数据:

Create table date_test ( string_date TEXT );

insert into date_test values
('5/17/1967'),
('3/7/1936'),
('10/20/1930');

转换字符串日期执行日期数据类型的查询(假设该年度始终是4位数字):

select string_date,
CASE
    WHEN length(Month) = 1 and length(Day) = 1 THEN DATE(Year || '-0' || Month || '-0' ||Day)
    WHEN length(Month) = 1 and length(Day) = 2 THEN DATE(Year || '-0' || Month || '-' ||Day)
    WHEN length(Month) = 2 and length(Day) = 1 THEN DATE(Year || '-' || Month || '-0' ||Day)
    WHEN length(Month) = 2 and length(Day) = 2 THEN DATE(Year || '-' || Month || '-' ||Day)
END AS date_as_date
from 
(select string_date
, substr(string_date, 0, INSTR(string_date, '/')) as Month
, substr(substr(string_date, INSTR(string_date, '/')+1), 0, INSTR(substr(string_date, INSTR(string_date, '/')+1), '/')) as Day
, substr(string_date, length(string_date)-3, length(string_date)) as Year
from date_test);


-- Result expected:
--------------------------------------
--string_date   date_as_date
--------------------------------------
--5/17/1967     1967-05-17
--3/7/1936      1936-03-07
--10/20/1930    1930-10-20

I found a way, maybe a bit convoluted but it works on my dataset on SQLite

Create the table and insert some test data:

Create table date_test ( string_date TEXT );

insert into date_test values
('5/17/1967'),
('3/7/1936'),
('10/20/1930');

The query that transform the string date do the date data type (assuming that the year is always 4 digits):

select string_date,
CASE
    WHEN length(Month) = 1 and length(Day) = 1 THEN DATE(Year || '-0' || Month || '-0' ||Day)
    WHEN length(Month) = 1 and length(Day) = 2 THEN DATE(Year || '-0' || Month || '-' ||Day)
    WHEN length(Month) = 2 and length(Day) = 1 THEN DATE(Year || '-' || Month || '-0' ||Day)
    WHEN length(Month) = 2 and length(Day) = 2 THEN DATE(Year || '-' || Month || '-' ||Day)
END AS date_as_date
from 
(select string_date
, substr(string_date, 0, INSTR(string_date, '/')) as Month
, substr(substr(string_date, INSTR(string_date, '/')+1), 0, INSTR(substr(string_date, INSTR(string_date, '/')+1), '/')) as Day
, substr(string_date, length(string_date)-3, length(string_date)) as Year
from date_test);


-- Result expected:
--------------------------------------
--string_date   date_as_date
--------------------------------------
--5/17/1967     1967-05-17
--3/7/1936      1936-03-07
--10/20/1930    1930-10-20
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文