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?
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.
You can set the year when the century changes like that
sp_configure 'two digit year cutoff', 2070
reconfigure
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