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:10 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Friday, August 5, 2011 8:55 AM
Points: 1, Visits: 38
This is a great article with very good followup comments. I have been working with SSRS for almost three years now, and every time i need some weird or exotic formula for the Sales Reports (Date requirements include today and the last 2 Months as well as same time last year!) I have to look up different formulas. I will definitely use this article in my SQL activities. Thank you for the time and effort as well as the great contribution!
Post #898470
Posted Wednesday, April 7, 2010 7:21 AM


SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: Tuesday, April 22, 2014 9:38 AM
Points: 1,525, Visits: 4,071
nakache (4/6/2010)
the fastest way to remove time from datetime is:
select CONVERT(datetime,floor(convert(float,getdate())))

Itzik Ben-Gan had a big post on Date Manipulation with benchmarking..(long time ago..)


I believe this is the article you were referring to? In there, he actually mentions that his favorite technique is the DADD method, although his tests show casting as an int to be very slightly faster.

Here's a performance evaluation by Gail Shaw on her blog of the different methods.

I won't make any claims as to which of these is faster, I'm sure they each win on occasion, but for the versatility, I'll stick with Dateadd/Datediff.


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 #898484
Posted Wednesday, April 7, 2010 7:29 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Thursday, July 17, 2014 4:22 PM
Points: 44, Visits: 1,661
mbarkell (4/7/2010)
On my system using the query for the first day of the week gives Monday instead of Sunday which is, of course, incorrect. Is this based on the locale of the system, or is that consistent behavior all together. After all, the first day of the week is always Sunday not Monday.


mbarkell,

check @@DATEFIRST (Transact-SQL) in BOL...

Arkware
Post #898493
Posted Wednesday, April 7, 2010 7:32 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Friday, September 7, 2012 6:46 AM
Points: 4, Visits: 25
Thank you, I thought it might be a sortof locale setting. @@DateFirst is a handy thing to keep in mind.
Post #898499
Posted Wednesday, April 7, 2010 7:33 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Monday, September 27, 2010 7:51 AM
Points: 15, Visits: 109
Very handy and nicely done article. I've come across situtations where I've needed to manipulate dates and will definitely keep this article handy.
Post #898500
Posted Wednesday, April 7, 2010 7:37 AM


SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: Tuesday, April 22, 2014 9:38 AM
Points: 1,525, Visits: 4,071
vinaypugalia (4/6/2010)
Great handy article !!

However, we can also achieve the same through -
SELECT CONVERT(DATETIME,DATEDIFF(dd,0,GETDATE()))

This way we can save the time taken to perform DATEADD operation though it would be quite marginal....


An interesting idea. It does seem to perform slightly better. Here's a proof using my 200K row Tally Table.

SELECT DATEADD(d, DATEDIFF(d,0,GETDATE()), 0) A
INTO #2
FROM Util..Tally

SELECT CONVERT(DATETIME,DATEDIFF(dd,0,GETDATE())) A
INTO #1
FROM Util..Tally

DROP TABLE #1
DROP TABLE #2


(200000 row(s) affected)

SQL Server Execution Times:
CPU time = 78 ms, elapsed time = 95 ms.

(200000 row(s) affected)

SQL Server Execution Times:
CPU time = 62 ms, elapsed time = 93 ms.


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 #898503
Posted Wednesday, April 7, 2010 7:40 AM
SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: Monday, June 30, 2014 7:39 AM
Points: 1,842, Visits: 998
If you are loading the date value into a datetime variable or a datetime column you only need to do the DATEDIFF.

DECLARE @dtDate datetime
SET @dtDate = datediff(d,0,getdate())
PRINT @dtDate

If you want to display the date value directly in a select then you'll need to convert it back to a datetime in some manner like the ones show in the article or in this thread. Note this method of stripping the time only works because SQL Server internally stores dates as numbers. If SQL Server were to change how it internally stores dates as numbers well then....who knows if these methods would work.
Post #898509
Posted Wednesday, April 7, 2010 7:45 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Thursday, July 17, 2014 4:22 PM
Points: 44, Visits: 1,661
Seth,

very nice. good explaination.

I have one question...

in the following code, isn't it returning the first of the week +/- 2 days?
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.

Thanks,

Arkware
Post #898514
Posted Wednesday, April 7, 2010 7:51 AM


SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: Tuesday, April 22, 2014 9:38 AM
Points: 1,525, Visits: 4,071
ArkWare (4/7/2010)
mbarkell (4/7/2010)
On my system using the query for the first day of the week gives Monday instead of Sunday which is, of course, incorrect. Is this based on the locale of the system, or is that consistent behavior all together. After all, the first day of the week is always Sunday not Monday.


mbarkell,

check @@DATEFIRST (Transact-SQL) in BOL...

Arkware


Great Question.

This is a tricky one. DATEDIFF doesn't actually honor datefirst or locale settings. Here's an article (also by Itzik Ben-Gan) that talks about it.

The reason you're getting monday is because 1/1/1900 was a monday. To adjust that particular query to get a sunday instead, you can use this:

SELECT DATEADD(wk, DATEDIFF(wk,0,GETDATE()), -1)

I should probably add that into the article.



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 #898522
Posted Wednesday, April 7, 2010 7:56 AM


SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: Tuesday, April 22, 2014 9:38 AM
Points: 1,525, Visits: 4,071
ArkWare (4/7/2010)
Seth,

very nice. good explaination.

I have one question...

in the following code, isn't it returning the first of the week +/- 2 days?
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.

Thanks,

Arkware


It is. That was a copy/paste error. Sorry about that. Those wk's should be d's.



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

Add to briefcase ««12345»»»

Permissions Expand / Collapse