[英]SQL SERVER Convert Date Format
Now in the column store string in three different format. 现在,在列中以三种不同的格式存储字符串。 Example such 这样的例子
How can I to convert the all the date to consolidate to stardard date like ddmmyyy? 如何将所有要合并的日期转换为像ddmmyyy这样的标准日期? Use Left 10 is not appropriate. 不适合使用Left 10。
Following is my solution for SQL2012: 以下是我针对SQL2012的解决方案:
DECLARE @Date AS TABLE(val VARCHAR(30));
INSERT INTO @Date VALUES('2/04/15 9.30-12');
INSERT INTO @Date VALUES('10/01/2015 10am - 1pm');
INSERT INTO @Date VALUES('17/03/15 1st appt');
SELECT TRY_PARSE(LEFT(val, CHARINDEX(' ', val)) AS DATE USING 'en-gb') AS Date FROM @Date;
And the output is: 输出为:
Will try to find out something for 2008 too. 还将尝试找出2008年的内容。 possibly. 可能的。
Use try_convert()
. 使用try_convert()
。 And then try_convert()
again. 然后再次try_convert()
。 Perhaps something like: 也许像这样:
select coalesce(try_convert(date, left(col, charindex(' ', col + ' ') - 1), 1),
try_convert(date, left(col, charindex(' ', col + ' ') - 1), 101),
try_convert(date, left(col, charindex(' ', col + ' ') - 1), 103),
. . .
) as dte
from t;
Just keep trying different styles. 只是继续尝试不同的样式。 Be careful that these are ordered. 请注意,这些命令已订购。 So, you have to decide if 02/01/2012
is February 1st or January 2nd. 因此,您必须确定02/01/2012
2月1日是2月1日还是1月2日。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.