How do I convert a string to a date type in SQL Server 2008 R2?
My string is formatted dd/mm/yyyy
I tried this
SELECT CAST('01/08/2014' AS DATE)
But that does the cast in mm/dd/yyyy
format.
You need the convert
function, where you can specify a format code:
select convert(datetime, '01/08/2014', 103)
The 103
means dd/mm/yyyy. See the docs.
Dateformat.
SET DATEFORMAT DMY ;
SELECT cast('01/08/2014' as date) ;
Convert.
SELECT convert(date, '01/08/2014', 103 ) ;
And for completeness, SQL Server 2012 and later has the following.
SELECT parse('01/08/2014' as date using 'en-NZ' ) ;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With