|
|
|
Forum Newbie
      
Group: General Forum Members
Last Login: Friday, August 05, 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!
|
|
|
|
|
SSCommitted
      
Group: General Forum Members
Last Login: Wednesday, November 07, 2012 4:08 PM
Points: 1,525,
Visits: 4,047
|
|
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
|
|
|
|
|
SSC Rookie
      
Group: General Forum Members
Last Login: Friday, May 17, 2013 11:32 AM
Points: 40,
Visits: 1,531
|
|
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
|
|
|
|
|
Forum Newbie
      
Group: General Forum Members
Last Login: Friday, September 07, 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.
|
|
|
|
|
Grasshopper
      
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.
|
|
|
|
|
SSCommitted
      
Group: General Forum Members
Last Login: Wednesday, November 07, 2012 4:08 PM
Points: 1,525,
Visits: 4,047
|
|
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
|
|
|
|
|
SSCommitted
      
Group: General Forum Members
Last Login: 2 days ago @ 7:27 AM
Points: 1,529,
Visits: 912
|
|
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.
|
|
|
|
|
SSC Rookie
      
Group: General Forum Members
Last Login: Friday, May 17, 2013 11:32 AM
Points: 40,
Visits: 1,531
|
|
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
|
|
|
|
|
SSCommitted
      
Group: General Forum Members
Last Login: Wednesday, November 07, 2012 4:08 PM
Points: 1,525,
Visits: 4,047
|
|
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
|
|
|
|
|
SSCommitted
      
Group: General Forum Members
Last Login: Wednesday, November 07, 2012 4:08 PM
Points: 1,525,
Visits: 4,047
|
|
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
|
|
|
|