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
 Resolved: Calculate the Days Between Two Dates

Author  Topic 

cnbhold
Starting Member

43 Posts

Posted - 2014-07-09 : 19:41:50
Have a table called Customers that has a datetime column called Submitted. I need a way to compare the Submitted field against the current date to tell if 5 days have pasted excluding weekends. In the sample data below, the record submitted on 7/2 is the record that should be returned in the query when compared to today's date 7/9.

FirstName........LastName.........Submitted
John................Miller...............7/01/2014
Mary................Smith..............7/02/2014
Bob.................Williams..........7/05/2014

Angel

ahmeds08
Aged Yak Warrior

737 Posts

Posted - 2014-07-10 : 05:08:41
can you try this
select * from customers
where datediff(day,submitted,getdate())=5

Javeed Ahmed
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2014-07-10 : 06:00:07
do you have a calendar table ?


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

cnbhold
Starting Member

43 Posts

Posted - 2014-07-10 : 06:45:33
I do not have a calendar table

Angel
Go to Top of Page

mmkrishna1919
Yak Posting Veteran

95 Posts

Posted - 2014-07-10 : 08:24:29
try this for total number of weekdays between two dates. Assuming only are saturday and sunday weekends.

Declare
@date1 datetime,
@date2 datetime
set @date1 = '01July2014'
set @date2 = GETDATE()
select
(
(
((
DATEDIFF(dd,
dateadd(dd
,(8-(DATEPART(weekday,@date1)))
,@date1
)
,@date2)
)+1)
/7
)
*5
)
+(DATEPART(weekday,@date2)-1)
+(7-DATEPART(weekday,@date1))




M.MURALI kRISHNA
Go to Top of Page

cnbhold
Starting Member

43 Posts

Posted - 2014-07-10 : 20:12:52
Here's a link to the solution I found. http://stackoverflow.com/questions/19765962/calculating-days-to-excluding-weekends-monday-to-friday-in-sql-server

Angel
Go to Top of Page
   

- Advertisement -