简体   繁体   中英

How to convert String date into another date format in SQL server?

I have a column which stores date in String format like (11/01/2010,2014-04-08,14-Oct-14).
I want to convert all this type of string dates in datetime/date format like (Dec 16 2014 3:40PM) I am trying like that

update #test set new_CHQ_DATE = CONVERT(varchar(20), CHQ_DATE, 105) where CHQ_DATE like  '__/__/____'
update #test set new_CHQ_DATE = CONVERT(varchar(20), CHQ_DATE, 105) where CHQ_DATE like  '____-__-__'
update #test set new_CHQ_DATE = CONVERT(varchar(20), CHQ_DATE, 105) where CHQ_DATE like  '__-___-__'

Bu

Your sample seems to work, although it relies on you knowing ahead of time all of the various different ways that people may have entered dates. SQL Server is pretty decent about converting strings into dates, if the string is in basically any NORMAL date recognizable format. This query seems to get more or less all of them (assuming that CHQ_DATE is some kind of VARCHAR and that new_CHQ_DATE is a DATETIME column):

SET DATEFORMAT dmy;
GO
UPDATE #Test
   SET new_CHQ_DATE = CONVERT(VARCHAR(20), CHQ_DATE, 105)
WHERE
   ISDATE(CHQ_DATE) = 1;

--reset DATEFORMAT back to whatever it was

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM