SQL Server 2008 DateTime and DateTime2 behavior – have you seen this?

Hi Friends,

You would know that SQL Server 2008 DateTime and DateTime2 data type amongst many new data types. Here is an interesting observation. Comments are welcome:

When you run the following code with the default date format setting, you get the right output:

declare @dt datetime='2012-11-04'
declare @dt2 datetime2='2012-11-04'
selectMONTH(@dt)as [Month-Datetime],DAY(@dt)as [Day-Datetime]
selectMONTH(@dt2)as [Month-Datetime2],DAY(@dt2)as [Day-Datetime2]
go

1_SQL_Server_DateTime_DateTime2_behavior

   

But when you change the format to dmy; you get a different output 🙂

setdateformat dmy
go
 
-- different with ANSI string format assignment
declare @dt datetime='2012-11-04'
declare @dt2 datetime2='2012-11-04'
selectMONTH(@dt)as [Month-Datetime],DAY(@dt)as [Day-Datetime]
selectMONTH(@dt2)as [Month-Datetime2],DAY(@dt2)as [Day-Datetime2]
go

2_SQL_Server_DateTime_DateTime2_behavior

Comments are welcome.

 

 

   

About Amit Bansal

Amit Bansal is always brainstorming around SQL Server. Despite working with SQL since 1997, he is amazed that he keeps learning new things every single day. SQL Server is AB's first love, and his wife does not mind that. He tries to share as much and spreads the SQL goodness. Internals and Performance Tuning excites him, and also gives him sleepless nights at times, simply because he is not a genius, but quite a hard worker and does not give up. It has been a long and exciting journey since 1997, you can read here: http://sqlmaestros.com/amit-bansal/ He is on Twitter: https://www.twitter.com/A_Bansal

View all posts by Amit Bansal →

Leave a Reply

Your email address will not be published.