1

Simple question cannot seem to get right.

I have a table with a date in this format 20/09/2012 as varchar.

I need to convert to a datetime

Tried various solutions but cannot get it right.

Any suggestions?

Thanks

3
  • 1
    Did you try CONVERT(datetime, '2012/09/20 00:00:00', 120) ? Also for date formatting options you can check msdn.microsoft.com/en-us/library/ms187928.aspx Commented Dec 6, 2013 at 10:16
  • 3
    Store it as a DATETIME in the first place ... Commented Dec 6, 2013 at 10:16
  • Using convert(datetime, '20/09/2012', 103) seems to work for me. Commented Dec 6, 2013 at 10:18

1 Answer 1

3

Using the CONVERT function with style 103 (British/European) seems to work for me:

DECLARE @input VARCHAR(20) = '20/09/2012'

SELECT
    CONVERT(DATETIME, @input, 103)

With this approach, you could convert all your VARCHAR dates to their proper datatype - DATETIME - and store them as DATETIME in the first place ....

See Aaron Bertrand great blog post on bad habits to kick : mis-handling date / range queries for more background info on why it's really really bad to keep storing dates as VARCHAR columns ....

Sign up to request clarification or add additional context in comments.

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.