Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase «««12345»»»

Date Manipulation with DATEADD/DATEDIFF Expand / Collapse
Author
Message
Posted Wednesday, April 7, 2010 7:59 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Friday, September 30, 2011 3:19 PM
Points: 2, Visits: 46
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.
Post #898530
Posted Wednesday, April 7, 2010 8:19 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Thursday, August 28, 2014 12:30 PM
Points: 3, Visits: 46
Thanks for taking the time. I've stored it in a handy place.
Post #898548
Posted Wednesday, April 7, 2010 9:24 AM
SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Tuesday, September 16, 2014 7:35 AM
Points: 2,817, Visits: 2,563
Date manipulation is always something I have to lookup. Hopefully this can help me remember it so I can just write it.

Thanks,
Post #898631
Posted Wednesday, April 7, 2010 9:34 AM
Valued Member

Valued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued Member

Group: General Forum Members
Last Login: Friday, November 26, 2010 7:48 AM
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
Post #898654
Posted Wednesday, April 7, 2010 9:37 AM
Valued Member

Valued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued Member

Group: General Forum Members
Last Login: Friday, November 26, 2010 7:48 AM
Points: 59, Visits: 86
using dd with
SELECT DATEADD(dd, DATEDIFF(dd,0,GETDATE()), 2) will give you two days from now
Post #898664
Posted Wednesday, April 7, 2010 10:33 AM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Monday, June 16, 2014 3:18 PM
Points: 140, Visits: 140
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...
Post #898752
Posted Wednesday, April 7, 2010 11:26 AM


SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: Tuesday, April 22, 2014 9:38 AM
Points: 1,519, Visits: 4,071
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
Post #898802
Posted Wednesday, April 7, 2010 12:12 PM
SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: Wednesday, August 13, 2014 6:34 AM
Points: 1,566, Visits: 1,851
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!
Post #898868
Posted Wednesday, April 7, 2010 6:48 PM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: General Forum Members
Last Login: Today @ 1:47 PM
Points: 35,215, Visits: 31,667
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."

(play on words) "Just because you CAN do something in T-SQL, doesn't mean you SHOULDN'T." --22 Aug 2013

Helpful Links:
How to post code problems
How to post performance problems
Post #899181
Posted Wednesday, April 7, 2010 7:25 PM


SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: Tuesday, April 22, 2014 9:38 AM
Points: 1,519, Visits: 4,071
Thanks.

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


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
Post #899193
« Prev Topic | Next Topic »

Add to briefcase «««12345»»»

Permissions Expand / Collapse