如何将字符串格式yyyy-mm-dd转换为dd / mm / yyyy

问题描述:

当前在将ms sql表中的字符串字段转换为所需的日期格式时遇到问题。

Ive currently got an issue with converting a string field in a ms sql table to my desired date format.

以前我一直使用

CONVERT(date,c.INP_DATE ,103)

但这给了我错误

[Err] 22007 - [SQL Server]Conversion failed when converting date and/or time from character string.

我有两个示例记录,分别是2013-09-18和2013-09-18 17:17:我通过mysql从导入中收到32.0000000。我需要将它们转换为dd / mm / yyyy(如果适用)。

two sample records i have are 2013-09-18 and 2013-09-18 17:17:32.0000000 which i receive from an import via mysql. I need these converted to dd/mm/yyyy with time where applicable.

任何大师都会提供帮助:)

Any help from any gurus out there is appreciated :)

DECLARE @a VARCHAR(50) = '2013-09-18'
, @b VARCHAR(50) = '2013-09-18 17:17:32.0000000'

SELECT CONVERT(VARCHAR(10),CAST(@a as DATE),103)
 , CONVERT(VARCHAR(10),CAST(@b as DATE),103)