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
 Help me sooner to fix this date's logic

Author  Topic 

jittu
Starting Member

1 Post

Posted - 2015-02-16 : 00:25:45
Hi,
I need to incorporate 2 date logic's in my stored procedure.
E.g
1st logic(7days logic)
1.when i select any date say for instance 16/02/2015 it should give me 7 days including weekend's i.e Saturday and Sunday.The result should be 23/02/2015

2nd logic(2 business days logic and it should include Saturdays and skip Sunday)
2.When i select any date say for instance 16/02/2015 it should give me 18/02/2015 and if i select Friday's date 13/02/2015 it should take Saturday and Monday but not Sunday.So, the result should be 16/02/2015


As i am new to SQL server need your help to fix it guys and all your logic's will be appreciated ..

waterduck
Aged Yak Warrior

982 Posts

Posted - 2015-02-16 : 00:41:18
[code]
declare @table table(col1 datetime)
insert into @table select
'20150216' union all select
'20150217' union all select
'20150218' union all select
'20150219' union all select
'20150220' union all select
'20150221' union all select
'20150222' union all select
'20150223' union all select
'20150224'

select *, DATEPART(weekday, col1)
from @table
where col1 <= DATEADD(DAY, DATEDIFF(DAY, -7, GETDATE()), 0)

select *, DATEPART(weekday, col1)
from @table
where col1 <= DATEADD(DAY, DATEDIFF(DAY, -7, GETDATE()), 0)
and cast(col1 as int)%7 <> 6
[/code]
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2015-02-16 : 01:45:49
You can find lot of 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
   

- Advertisement -