Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Understanding Days in SQL

Author  Topic 

jrobin747
Starting Member

48 Posts

Posted - 2013-08-23 : 10:34:48
I'm trying to understand the logic of days in SQL
If today, I wanted to get a count for a day, to accurately do that
would my date range be yesterdays date and tomorrows date.

MuMu88
Aged Yak Warrior

549 Posts

Posted - 2013-08-23 : 11:04:15
Depends on what you need, try this for your self and then decide:
[CODE]
DECLARE @BeginDate DATE = '20130801'

SELECT DATEDIFF(dd, @BeginDate, GetDate());

[/CODE]

Refer to http://technet.microsoft.com/en-us/library/ms189794.aspx
for more information.
Go to Top of Page

Ifor
Aged Yak Warrior

700 Posts

Posted - 2013-08-23 : 11:05:23
[code]
WHERE YourDate >= DATEADD(d, DATEDIFF(d, 0, CURRENT_TIMESTAMP), 0) -- today at 00:00:00
AND YourDate < DATEADD(d, DATEDIFF(d, 0, CURRENT_TIMESTAMP), 1) -- tomorrow at 00:00:00
[/code]
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2013-08-26 : 06:42:48
You can find more examples at http://beyondrelational.com/modules/2/blogs/70/posts/10899/understanding-datetime-column-part-iii.aspx

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2013-08-27 : 01:41:02
see the comparison of different date range logic here

http://visakhm.blogspot.in/2012/12/different-ways-to-implement-date-range.html



------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page
   

- Advertisement -