Lets say I have a datetime from my database, such as: 2011-10-24 00:00:00.000. I would like to convert this into 24/10/2011, but I can’t find the native SQL Server way to do this, and I’d rather no do this in the code after I run my query.
I’ve tried doing something as follows (as per the manual)
select CONVERT(datetime, '2011-10-24 00:00:00.000', 103);
But that doesn’t work out, my result is:
Msg 242, Level 16, State 3, Line 1
The conversion of a char data type to a datetime data type resulted in an out-of-range datetime value.
I suspect this might be due to the decimal precision, or the time part of this, but I’m not sure how to go about this without resulting to some string-hacking-regex-replace nonsense (and I’d rather no do that, I might as well end up doing the conversion after the query if I do that).
If I read the manual correctly, you should be using stlye 120 “ODBC canonical” – no??
Try:
Works on my machine 🙂 This gives you a SQL Server
DATETIMEto work with.If you want to convert that further into
24/10/2011, use a secondCONVERTto convert into aVARCHARstring:That gives me: