1

I need to (if its possible) convert a column that stores the date as a string in the following format Monday October 19, 2015 to an usable datetime for example 01/10/2015

Every cast and convert I have tried to run it through just returns

Conversion failed when converting date and/or time from character string.

Nothing I can do about the saved format as its part of a fixed process.

2 Answers 2

5

Chop off the day:

DECLARE @S VARCHAR(32) = 'Monday October 19, 2015'

SELECT CAST(SUBSTRING(@S, CHARINDEX(' ', @S) + 1, LEN(@S)) AS DATETIME)

> 2015-10-19 00:00:00.000
Sign up to request clarification or add additional context in comments.

1 Comment

Clever. Didnt event think of stripping it down. Thank you!
0

You should manipulate the string to this format - "DD/MON/YYYY", where DD is day, MON is the first three letters of the months name, and YYYY is the year. You could do that easily do that using string functions, and from there you can insert the date into the DB using conventional conversions, such as the ones listed in this post - Convert DD-Mon-YYYY to DD/MM/YYYY.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.