简体   繁体   English

如何在SQL Server中将字符串日期转换为另一种日期格式?

[英]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). 我有一个以字符串格式存储日期的列,例如(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 我想将所有这种类型的字符串日期转换为日期时间/日期格式,例如(Dec 16 2014 3:40 PM)

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. 如果字符串基本上是任何NORMAL日期可识别的格式,则SQL Server可以很好地将字符串转换为日期。 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): 该查询似乎或多或少地都获得了它们(假设CHQ_DATE是某种VARCHAR,而new_CHQ_DATE是DATETIME列):

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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