我有一个名为 calendar 的表,其中包含 DateTo 列 格式为 2014-02-19T16:00:00 (varchar 并且无法更改) 我想将时间设置为 13:00
我已经尝试过
UPDATE calendar
SET DateTo = (SUBSTRING(DateTo FROM 1 FOR 11) + '13:00')
WHERE SUBSTRING(DateFrom FROM 1 FOR 10) = '2014-02-19'
认为这会给我:
2014-02-19T13:00
但它返回的是 2027?
最佳答案
你可以这样做
update
calendar
set DateTo = concat(
substring_index(DateTo,'T',1),'T','13:00'
)
where substring_index(DateTo,'T',1) = '2014-02-19'
;
<强> DEMO
正如MatBailie建议的那样,而不是
where substring_index(DateTo,'T',1) = '2014-02-19'
更好用
WHERE DateTo LIKE '2014-02-19T%'
如果 DateTo 上有索引,速度会快得多
关于mysql - 想要一个 sql 来更新子字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23406133/