문제

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