Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Date Manipulation with DATEADD/DATEDIFF


Date Manipulation with DATEADD/DATEDIFF

Author
Message
bcool
bcool
Forum Newbie
Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)

Group: General Forum Members
Points: 2 Visits: 58
Sorry - duplicate post (someone just beat me to it!) :-)


Good article!

I think there might be some typos in the examples for finding dates in the past & future....this is what was shown:

SELECT DATEADD(wk, DATEDIFF(wk,0,GETDATE()), 2)
--: 2010-03-01 00:00:00.000 Start of the day 2 days from now
SELECT DATEADD(wk, DATEDIFF(wk,0,GETDATE()), -2)
--: 2010-02-25 00:00:00.000 Start of the day 2 days ago.

I believe it should be:

SELECT DATEADD(dd, DATEDIFF(dd,0,GETDATE()), 2)
--: 2010-03-01 00:00:00.000 Start of the day 2 days from now
SELECT DATEADD(dd, DATEDIFF(dd,0,GETDATE()), -2)
--: 2010-02-25 00:00:00.000 Start of the day 2 days ago.
Larry Watson
Larry Watson
Forum Newbie
Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)

Group: General Forum Members
Points: 3 Visits: 80
Thanks for taking the time. I've stored it in a handy place.
Daniel Bowlin
Daniel Bowlin
SSCrazy
SSCrazy (3K reputation)SSCrazy (3K reputation)SSCrazy (3K reputation)SSCrazy (3K reputation)SSCrazy (3K reputation)SSCrazy (3K reputation)SSCrazy (3K reputation)SSCrazy (3K reputation)

Group: General Forum Members
Points: 2950 Visits: 2629
Date manipulation is always something I have to lookup. Hopefully this can help me remember it so I can just write it.

Thanks,
Old_D
Old_D
Valued Member
Valued Member (59 reputation)Valued Member (59 reputation)Valued Member (59 reputation)Valued Member (59 reputation)Valued Member (59 reputation)Valued Member (59 reputation)Valued Member (59 reputation)Valued Member (59 reputation)

Group: General Forum Members
Points: 59 Visits: 86
SELECT DATEADD(wk, DATEDIFF(wk,0,GETDATE()), 2) will produce today's date not Start of the day 2 days from now as you stated.
Good idea to have scripts for dates
Old_D
Old_D
Valued Member
Valued Member (59 reputation)Valued Member (59 reputation)Valued Member (59 reputation)Valued Member (59 reputation)Valued Member (59 reputation)Valued Member (59 reputation)Valued Member (59 reputation)Valued Member (59 reputation)

Group: General Forum Members
Points: 59 Visits: 86
using dd with
SELECT DATEADD(dd, DATEDIFF(dd,0,GETDATE()), 2) will give you two days from now
Elaine Shafer-401422
Elaine Shafer-401422
SSC-Enthusiastic
SSC-Enthusiastic (152 reputation)SSC-Enthusiastic (152 reputation)SSC-Enthusiastic (152 reputation)SSC-Enthusiastic (152 reputation)SSC-Enthusiastic (152 reputation)SSC-Enthusiastic (152 reputation)SSC-Enthusiastic (152 reputation)SSC-Enthusiastic (152 reputation)

Group: General Forum Members
Points: 152 Visits: 149
I frequently use SELECT CAST(FLOOR(CAST(<yourDateHere> AS FLOAT)) AS DATETIME)

Interestingly, rounding happens near midnight. The following returns '2010-04-08 00:00:00.000':

DECLARE @Date DATETIME = '2010-04-07 23:59:59.999'

SELECT CAST(FLOOR(CAST(@Date AS FLOAT)) AS DATETIME)

Not sure if this timestamp would actually happen...
Garadin
Garadin
SSCommitted
SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)

Group: General Forum Members
Points: 1523 Visits: 4107
Elaine Shafer-401422 (4/7/2010)
I frequently use SELECT CAST(FLOOR(CAST(<yourDateHere> AS FLOAT)) AS DATETIME)

Interestingly, rounding happens near midnight. The following returns '2010-04-08 00:00:00.000':

DECLARE @Date DATETIME = '2010-04-07 23:59:59.999'

SELECT CAST(FLOOR(CAST(@Date AS FLOAT)) AS DATETIME)

Not sure if this timestamp would actually happen...


As mentioned in the article, Datetime is only accurate to 3ms. 999 and 998 both round up to 000. It's not your float conversion doing the rounding, it's the limitations of the datetime data type. Run this:

DECLARE @Date DATETIME = '2010-04-07 23:59:59.999'
SELECT @date
SELECT CAST(FLOOR(CAST(@Date AS FLOAT)) AS DATETIME)



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
Carla Wilson-484785
Carla Wilson-484785
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: 1709 Visits: 1949
Thanks for a good article. I've been using the CONVERT(CONVERT) technique mentioned by lhowe.

lhowe (4/7/2010)
Another method to strip the time from a date/time value and keep it as a datetime type would be: CONVERT(datetime,CONVERT(varchar,GetDate(),101))


The value of the DADD technique is the ability to apply it to weeks, months, years.
Thanks again!
Jeff Moden
Jeff Moden
SSC-Forever
SSC-Forever (44K reputation)SSC-Forever (44K reputation)SSC-Forever (44K reputation)SSC-Forever (44K reputation)SSC-Forever (44K reputation)SSC-Forever (44K reputation)SSC-Forever (44K reputation)SSC-Forever (44K reputation)

Group: General Forum Members
Points: 44806 Visits: 39847
Nice done, Seth. Good clear article. Heh... it's funny how these things happen... I just got done demonstrating to the folks at work how VARCHAR conversions use twice the CPU that DADD conversions 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.
Although they tell us that they want it real bad, our primary goal is to ensure that we dont actually give it to them that way.
Although change is inevitable, change for the better is usually not.
Just because you can do something in PowerShell, doesnt mean you should. Wink

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
Garadin
Garadin
SSCommitted
SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)

Group: General Forum Members
Points: 1523 Visits: 4107
Thanks.

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

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
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