SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Date Manipulation with DATEADD/DATEDIFF


Date Manipulation with DATEADD/DATEDIFF

Author
Message
SQLRNNR
SQLRNNR
SSC Guru
SSC Guru (67K reputation)SSC Guru (67K reputation)SSC Guru (67K reputation)SSC Guru (67K reputation)SSC Guru (67K reputation)SSC Guru (67K reputation)SSC Guru (67K reputation)SSC Guru (67K reputation)

Group: General Forum Members
Points: 67609 Visits: 18570
Garadin (4/7/2010)
Thanks.

Nothing like actually having an article published that lets you see all the things that you forgot to add to it! Hehe


I thought of the same thing with my first article publication. It is a nice learning tool.



Jason AKA CirqueDeSQLeil
I have given a name to my pain...
MCM SQL Server, MVP


SQL RNNR

Posting Performance Based Questions - Gail Shaw

SQLJeff
SQLJeff
Grasshopper
Grasshopper (10 reputation)Grasshopper (10 reputation)Grasshopper (10 reputation)Grasshopper (10 reputation)Grasshopper (10 reputation)Grasshopper (10 reputation)Grasshopper (10 reputation)Grasshopper (10 reputation)

Group: General Forum Members
Points: 10 Visits: 1
And what about the old CONVERT(VARCHAR(12),GETDATE(),101), will this work in some cases?
SQLJeff
SQLJeff
Grasshopper
Grasshopper (10 reputation)Grasshopper (10 reputation)Grasshopper (10 reputation)Grasshopper (10 reputation)Grasshopper (10 reputation)Grasshopper (10 reputation)Grasshopper (10 reputation)Grasshopper (10 reputation)

Group: General Forum Members
Points: 10 Visits: 1
I concur... thanks Seth, great article and I have added these tips and documentation to my notes.
Hanri Naude
Hanri Naude
SSC Rookie
SSC Rookie (30 reputation)SSC Rookie (30 reputation)SSC Rookie (30 reputation)SSC Rookie (30 reputation)SSC Rookie (30 reputation)SSC Rookie (30 reputation)SSC Rookie (30 reputation)SSC Rookie (30 reputation)

Group: General Forum Members
Points: 30 Visits: 81
Hi, Great to see all the ways to manipulate dates in SQL.

Here is another way of doing it rather quickly.

SELECT CAST(CAST(GETDATE() AS CHAR(11)) AS DATETIME)

Cool stuff,
Hanri

Whenever I get sad, I stop being sad and be awesome instead… true story!
Hanri Naude
Hanri Naude
SSC Rookie
SSC Rookie (30 reputation)SSC Rookie (30 reputation)SSC Rookie (30 reputation)SSC Rookie (30 reputation)SSC Rookie (30 reputation)SSC Rookie (30 reputation)SSC Rookie (30 reputation)SSC Rookie (30 reputation)

Group: General Forum Members
Points: 30 Visits: 81
hi,
You can change the first day of the week by using the @@DATEFIRST keyword.

http://msdn.microsoft.com/en-us/library/ms181598(SQL.90).aspx

Regards,
Hanri

Whenever I get sad, I stop being sad and be awesome instead… true story!
Jeff Moden
Jeff Moden
SSC Guru
SSC Guru (218K reputation)SSC Guru (218K reputation)SSC Guru (218K reputation)SSC Guru (218K reputation)SSC Guru (218K reputation)SSC Guru (218K reputation)SSC Guru (218K reputation)SSC Guru (218K reputation)

Group: General Forum Members
Points: 218556 Visits: 41996
SQLJeff (4/8/2010)
And what about the old CONVERT(VARCHAR(12),GETDATE(),101), will this work in some cases?


Most likely... but the problem with that is as I previously stated... it uses twice as much CPU time and takes twice as long duration wise. If you're only working with a handful of rows, you certainly won't notice the difference. BUT, if you're working with many millions of rows like I usually have to, combined with other CPU saving methods, it makes all the difference in the world. Every microsecond counts for the stuff I usually have to do.

--Jeff Moden

RBAR is pronounced ree-bar and is a Modenism for Row-By-Agonizing-Row.
First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column.
If you think its expensive to hire a professional to do the job, wait until you hire an amateur. -- Red Adair

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
Mike M - DBA2B
Mike M - DBA2B
Old Hand
Old Hand (370 reputation)Old Hand (370 reputation)Old Hand (370 reputation)Old Hand (370 reputation)Old Hand (370 reputation)Old Hand (370 reputation)Old Hand (370 reputation)Old Hand (370 reputation)

Group: General Forum Members
Points: 370 Visits: 969
Great article, Seth! Seeing as how I've tried method after method for accomplishing this and I've never been pleased with what I've done, I think this is clever, elegant, and darned handy. Thanks for sharing!

Regards,

Mike M
Garadin
Garadin
SSCertifiable
SSCertifiable (7.1K reputation)SSCertifiable (7.1K reputation)SSCertifiable (7.1K reputation)SSCertifiable (7.1K reputation)SSCertifiable (7.1K reputation)SSCertifiable (7.1K reputation)SSCertifiable (7.1K reputation)SSCertifiable (7.1K reputation)

Group: General Forum Members
Points: 7081 Visits: 4107
Hanri Naude (4/8/2010)
hi,
You can change the first day of the week by using the @@DATEFIRST keyword.

http://msdn.microsoft.com/en-us/library/ms181598(SQL.90).aspx

Regards,
Hanri


You can change the day of the week, but as I mentioned in an earlier comment, DATEDIFF is not affected by DATEFIRST settings. I'll add a section about that into the article when I get a moment and can submit that and a few other changes.

Also, as Jeff mentioned, converting to a char/varchar and then back to datetime is considerably slower... but we're talking about fractions of a second per row, so if you're only doing a couple it's not a big deal. For instance, doing anything to GETDATE() to store in a variable... not gonna make much of a difference. But If you need to do it to the column of a table with a million rows, you'll definitely see the difference.

Seth Phelabaum
Consistency is only a virtue if you're not a screwup. ;-)

Links: How to Post Sample Data :: Running Totals :: Tally Table :: Cross Tabs/Pivots :: String Concatenation
Elaine Shafer-401422
Elaine Shafer-401422
SSC-Enthusiastic
SSC-Enthusiastic (164 reputation)SSC-Enthusiastic (164 reputation)SSC-Enthusiastic (164 reputation)SSC-Enthusiastic (164 reputation)SSC-Enthusiastic (164 reputation)SSC-Enthusiastic (164 reputation)SSC-Enthusiastic (164 reputation)SSC-Enthusiastic (164 reputation)

Group: General Forum Members
Points: 164 Visits: 149
Is this because SQL Server stores dates as a numeric value? Then CONVERT to VARCHAR() would cause an implicit conversion?
Slick84
Slick84
SSCommitted
SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)

Group: General Forum Members
Points: 1678 Visits: 1163
Thank you for the article. This function has always thrown me off on SQL server.

I dreaded dates before this! Thanks again.

--
Hehe
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search