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
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 ....
CONVERT(datetime, '2012/09/20 00:00:00', 120)? Also for date formatting options you can check msdn.microsoft.com/en-us/library/ms187928.aspxDATETIMEin the first place ...convert(datetime, '20/09/2012', 103)seems to work for me.