1

I have a string Sunday, November 19, 2017 in Varchar.

How can I convert this to Date Format - dddd, MMMM, yyyy?

gotqn
  • 42,737
  • 46
  • 157
  • 243
Akshay Mishra
  • 35
  • 1
  • 5
  • 3
    what have you tried so far? post your code there. – aircraft Jan 12 '18 at 07:43
  • 1
    Possible duplicate of [Convert varchar into datetime in SQL Server](https://stackoverflow.com/questions/1509977/convert-varchar-into-datetime-in-sql-server) – sumit chauhan Jan 12 '18 at 07:49
  • @sumitchauhan I don't see the answer to OP's question in this linked question, not with this 'long' date format. – Pac0 Jan 12 '18 at 08:02

1 Answers1

0

Are you looking for this:

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

or this:

SELECT FORMAT(CAST(SUBSTRING(@MyDate, CHARINDEX(',', @MyDate) + 2, 32) AS DATETIME2), 'dddd, MMMM, yyyy'); -- Sunday, November, 2017
gotqn
  • 42,737
  • 46
  • 157
  • 243
  • Thanks.. It worked for me. To get the desired result, did a minor change in the script. SELECT FORMAT(CAST(SUBSTRING(@MyDate, CHARINDEX(',', @MyDate) + 2, 32) AS DATETIME2), 'dddd, MMMM dd, yyyy'); -- Sunday, November 12, 2017 – Akshay Mishra Jan 13 '18 at 09:28