将具有日期名称的Varchar / String转换为Sql Server中的Date

时间:2018-01-12 07:42:08

标签: sql-server tsql casting sql-server-2014

我在Varchar中有一个字符串Sunday, November 19, 2017

如何将其转换为日期格式 - dddd, MMMM, yyyy

1 个答案:

答案 0 :(得分:0)

你在找这个:

DECLARE @MyDate VARCHAR(32) = 'Sunday, November 19, 2017';

SELECT CONVERT(VARCHAR(32), CAST(SUBSTRING(@MyDate, CHARINDEX(',', @MyDate) + 2, 32) AS DATETIME2), 105);  -- 19-11-2017

或者这个:

SELECT FORMAT(CAST(SUBSTRING(@MyDate, CHARINDEX(',', @MyDate) + 2, 32) AS DATETIME2), 'dddd, MMMM, yyyy'); -- Sunday, November, 2017