在工作日
Sunday=1
Monday=2
Tuesday=3
Wednesday=4
Thursday=5
Friday=6
Saturday=7
我有当前的字符串
Saturdays at 13:35 PDT
我想INSERT
进入DATETIME
列,使其日期和时间仅像这样:
DATETIME
0000-01-01 13:35:00 //Sundays at 13:35 PDT
0000-01-02 13:35:00 //Mondays at 13:35 PDT
0000-01-03 13:35:00 //Tuesdays at 13:35 PDT
0000-01-04 13:35:00 //Wednesdays at 13:35 PDT
0000-01-05 13:35:00 //Thursdays at 13:35 PDT
0000-01-06 13:35:00 //Fridays at 13:35 PDT
0000-01-07 13:35:00 //Saturdays at 13:35 PDT
我已经考虑过使用以下内容
INSERT into TABLE(column) VALUES (STR_TO_DATE('Saturdays at 13:35', '%Ws at %H:%i'))
但是它总是给我NULL
的价值,我也想将时区从 PDT 更改为 UTC ,但是我不知道如何使用STR_TO_DATE()
函数
答案 0 :(得分:0)
在工作日使用CASE
可能对此有所帮助。
然后CONCAT
时间到了。
示例:
SELECT
CONCAT(
case SUBSTRING(weekday_at_time, 1, LOCATE(' ',weekday_at_time)-1)
when 'Sundays' then '0000-01-01 '
when 'Mondays' then '0000-01-02 '
when 'Tuesdays' then '0000-01-03 '
when 'Wednesdays' then '0000-01-04 '
when 'Thursdays' then '0000-01-05 '
when 'Fridays' then '0000-01-06 '
when 'Saturdays' then '0000-01-07 '
end,
STR_TO_DATE(SUBSTRING(weekday_at_time,length(weekday_at_time)-4,5),'%H:%i')
) AS "DATETIME"
FROM (
-- Sample data
select 'Sundays at 14:45' as weekday_at_time
union all select 'Saturdays at 13:35'
) AS Samples
在SQL Fiddle here上进行测试
但这是假设您将其更新为VARCHAR列。
因为将'0000'年的datetimestamp强制转换为datetime可以得出不同的结果。
例如:
select cast('0000-01-01 13:35:00' as datetime) as dt
返回:
-0001-12-30T13:35:00Z
如果将日期更新为DATETIME,将年份更改为“ 1900”可能更安全。
在SQL Fiddle here