質問

I have a table called calendar with a column DateTo This has format 2014-02-19T16:00:00 (varchar and can not be changed) I would like to set time to 13:00

I've tried

UPDATE calendar 
SET DateTo = (SUBSTRING(DateTo FROM 1 FOR 11) + '13:00') 
WHERE SUBSTRING(DateFrom FROM 1 FOR 10) = '2014-02-19'

Thinking this would give me:

2014-02-19T13:00

but instead it returns 2027?

役に立ちましたか?

解決

You can do as

update 
calendar
set DateTo = concat(
  substring_index(DateTo,'T',1),'T','13:00'
)
where substring_index(DateTo,'T',1) = '2014-02-19'
;

DEMO

As MatBailie suggested instead of

where substring_index(DateTo,'T',1) = '2014-02-19'

its better to use

WHERE DateTo LIKE '2014-02-19T%'

This will be significantly faster if there is an index on DateTo

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top