Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

dates before 1950 equate future date

I need to convert my string date to datetime type.

Here's one date scenario:

'19-Apr-36'

DECLARE @strDate VARCHAR(25)
SELECT @strDate = '19-Apr-36'

SELECT CAST(@strDate as datetime)

results in a future date of 2036-04-19

how is this normally handled and what other caveats are there?

like image 303
Rod Avatar asked Mar 15 '26 04:03

Rod


2 Answers

SQL Server interprets the year 36 as in the current century by default. You'll need to explicitly state 1936 or change the two digit year cutoff option.

Edit: Just to clarify - you don't want to change the setting as it's serverwide and will cause more confusion than it'll help you. Go with explicit dates preferably.

like image 120
Mark S. Rasmussen Avatar answered Mar 16 '26 18:03

Mark S. Rasmussen


You can set the year when the century changes like that

sp_configure 'two digit year cutoff', 2070
reconfigure
like image 30
juergen d Avatar answered Mar 16 '26 16:03

juergen d



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!