Login or Sign Up to become a member!
LessThanDot Sit Logo

LessThanDot

Data Management

Less Than Dot is a community of passionate IT professionals and enthusiasts dedicated to sharing technical knowledge, experience, and assistance. Inside you will find reference materials, interesting technical discussions, and expert tips and commentary. Once you register for an account you will have immediate access to the forums and all past articles and commentaries.

LTD Social Sitings

Lessthandot twitter Lessthandot Linkedin Lessthandot friendfeed Lessthandot facebook Lessthandot rss

Note: Watch for social icons on posts by your favorite authors to follow their postings on these and other social sites.

Your profile

Authors

Search

XML Feeds

Google Ads

« IsNumeric, IsInt, IsNumberWindows Azure, SQL Azure and .NET Services pricing announced »
comments
Rate Post:
submit to reddit Digg!FacebookDotnetkicks

I have seen enough questions about this lately and this means that it is time for a blogpost. SQL Server 2008 has a bunch of new data types and one of them is the date datatype.
If you don't care for the time portion of the date you can now use the date data type and save 5 bytes per row compared to datetime. I know that there is smalldatetime which only takes up 4 bytes but I myself could not use that because we have data that goes back to 1896 and thus can't be stored in smalldatetime

So take a look at this code

  1. DECLARE @d DATETIME
  2. SELECT @d = GETDATE()
  3.  
  4. SELECT @d  =@d+1
  5. SELECT @d


To convert this to SQL Server 2008 logically you would think that all you had to do is change datetime to date. Go ahead...run it...make my day

  1. DECLARE @d DATE
  2. SELECT @d = GETDATE()
  3.  
  4. SELECT @d  =@d+1
  5. SELECT @d

And here is the message
Server: Msg 206, Level 16, State 2, Line 4
Operand type clash: date is incompatible with int

Now what you can do is use dateadd instead

  1. DECLARE @d DATE
  2. SELECT @d = GETDATE()
  3.  
  4. SELECT @d  = DATEADD(DAY,1,@d)
  5. SELECT @d

So that will work with both date and datetime (should work with all the date datatypes) and it is clear what you are doing.

But wait....scroll down in the next 5 seconds and you will get another option as a bonus.

What about this?

  1. DECLARE @d DATE
  2. SELECT @d = GETDATE() +1
  3.  
  4. SELECT @d

That does the addition before assignment.

But wait....scroll down in the next 5 seconds and you will get another option which is even shorter as a bonus.

Here is another version which is a little shorter

  1. DECLARE @d DATE = GETDATE() +1
  2. SELECT @d

Even though the last two versions are shorter, I would opt for using dateadd. With dateadd you know what you are doing, what does +1 mean? Are you adding days, hours or what...it is not clear from just looking at the code without running it




*** If you have a SQL related question try our Microsoft SQL Server Programming forum or our Microsoft SQL Server Admin forum

About the Author

User bio imageDenis has been working with SQL Server since version 6.5. Although he worked as an ASP/JSP/ColdFusion developer before the dot com bust, he has been working exclusively as a database developer/architect since 2002. In addition to English, Denis is also fluent in Croatian and Dutch, but he can curse in many other languages and dialects (just ask the SQL optimizer) He lives in Princeton, NJ with his wife and three kids.
Social SitingsTwitterFacebookLinkedInHomePageLTD RSS Feed
1737 views
submit to reddit Digg!FacebookDotnetkicks

Comments and Feedback

1 comment

Comment from: onpnt [Member] Email
*****
Great post Denis!

The question that needs to be asked to the individuals having these types of problems is should they use them if they do not educate themselves on them first.

Date is a perfect example. Great and long awaited data type. Let's use it NOW!!! Oh wait, all my code doesn't work any more.

Just because the button is there doesn't mean you press it before finding out what it does "completely" :-)
07/15/09 @ 11:56

Leave a comment


Your email address will not be revealed on this site.

Your URL will be displayed.
(Line breaks become <br />)
(Name, email & website)
(Allow users to contact you through a message form (your email will not be revealed.)