Home > other >  Cannot cast a specific datetime string into a date
Cannot cast a specific datetime string into a date

Time:11-09

Why can I cast this as a date:

select cast('3/11/2021 12:00:00 AM' as date)

But not this:

select cast('26/02/2021 12:00:00 AM' as date)

Thanks for your help

CodePudding user response:

Cast uses "default" format (#101) based on SQL Server locale, whereas CONVERT - as suggested by Dale K allows you to specify the date format that you use.

In your example I believe that: 3/11 is March 11th, and 26/02 is a second day of 26th month which simply doesn't work.

Example using convert:

select convert(datetime,'26/02/2021',103)
select convert(datetime,'02/26/2021',101)
  • Related