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.
Author |
Topic |
cnbhold
Starting Member
43 Posts |
Posted - 2013-04-12 : 15:36:38
|
How would I create a function that would return a list 10 days from the current date minus the weekends? If the current date was 4/12/2013, I would need to list the following available dates4/15/20134/16/20034/17/20034/18/20034/19/20034/22/20034/23/20034/24/20034/25/20034/16/2003Angel |
|
James K
Master Smack Fu Yak Hacker
3873 Posts |
Posted - 2013-04-12 : 16:30:28
|
quote: Originally posted by cnbhold How would I create a function that would return a list 10 days from the current date minus the weekends? If the current date was 4/12/2013, I would need to list the following available dates4/15/20134/16/20034/17/20034/18/20034/19/20034/22/20034/23/20034/24/20034/25/20034/16/2003Angel
I am assuming that the year for rows 2-10 being 2003 is a typo; and also that the date for the last row being 16 is a typo. Under all those assumptions you can use something like this:SELECT TOP (10) DATEADD(dd,DATEDIFF(dd,0,GETDATE())+N,0)FROM (VALUES (1),(2),(3),(4),(5),(6),(7), (8),(9),(10),(11),(12),(13),(14) ) AS C(N)WHERE (DATEDIFF(dd,0,GETDATE())+N)%7 < 5ORDER BY 1; |
 |
|
Lamprey
Master Smack Fu Yak Hacker
4614 Posts |
Posted - 2013-04-12 : 16:34:25
|
Will this work?WITH DayCte AS ( SELECT CAST(SYSDATETIME() AS DATE) AS MyDate UNION ALL SELECT DATEADD(DAY, 1, MyDate) FROM DayCte WHERE MyDate <= DATEADD(DAY, 15, SYSDATETIME()) ) SELECT TOP 10 MyDateFROM DayCteWHERE DATEDIFF(DAY, 0, MyDate) % 7 NOT IN (5, 6) |
 |
|
cnbhold
Starting Member
43 Posts |
Posted - 2013-04-12 : 23:16:30
|
Lamprey, here's what I have so far. The date 4/18/2013 should not be in the final results since it's the orginal date I'm needing to add 10 days minus the weekends to.SELECT Schedule_DTFROM MigrationSchedule_DT-----------------------2013-04-18 21:46:40.340(1 row(s) affected)DECLARE @Schedule_DT DATETIME = (SELECT Schedule_DT FROM Migration);WITH DayCte AS ( SELECT CAST(@Schedule_DT AS DATE) AS MyDate UNION ALL SELECT DATEADD(DAY, 1, MyDate) FROM DayCte WHERE MyDate <= DATEADD(DAY, 15, @Schedule_DT) ) SELECT TOP 10 MyDateFROM DayCteWHERE DATEDIFF(DAY, 0, MyDate) % 7 NOT IN (5, 6)MyDate----------2013-04-182013-04-192013-04-222013-04-232013-04-242013-04-252013-04-262013-04-292013-04-302013-05-01(10 row(s) affected) |
 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2013-04-13 : 01:54:26
|
[code]DECLARE @Schedule_DT DATETIME = (SELECT Schedule_DT FROM Migration);SELECT TOP 10 [Date]FROM dbo.CalendarTable(@Schedule_DT+1,@Schedule_DT + 15,1,0)ORDER BY [Date][/code]dbo.CalendarTable function is as given belowhttp://visakhm.blogspot.in/2010/02/generating-calendar-table.html------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/https://www.facebook.com/VmBlogs |
 |
|
cnbhold
Starting Member
43 Posts |
Posted - 2013-04-13 : 12:29:44
|
That worked!! Thanks Visakh16. |
 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2013-04-15 : 02:08:33
|
welcome------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/https://www.facebook.com/VmBlogs |
 |
|
|
|
|
|
|