Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert from varchar formatted as 'dd/mm/yyyy hh:mm:ss' to DateTime

I'm stuck with the following. How do I convert strings that are formatted like the following into DateTime type?

SELECT CONVERT(DATETIME,LEFT('26/03/2012 00:00:00',10))

Error on the above is as follows:

The conversion of a varchar data type to a datetime data type resulted in an out-of-range value.

like image 980
whytheq Avatar asked Oct 19 '25 13:10

whytheq


2 Answers

You can use the CONVERT function with style 103 (dd/mm/yyyy), see this link for further details.

SELECT CONVERT(DATETIME,'26/03/2012 00:00:00',103)
like image 142
Alex Avatar answered Oct 21 '25 04:10

Alex


You can use SET DATEFORMAT:

SET DATEFORMAT dmy;

SELECT CAST('26/03/2012 00:00:00' AS DATETIME);

LiveDemo

like image 24
Lukasz Szozda Avatar answered Oct 21 '25 05:10

Lukasz Szozda



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!