Author |
Topic |
esthera
Master Smack Fu Yak Hacker
1410 Posts |
Posted - 2010-06-30 : 06:55:48
|
i have a table CREATE TABLE [dbo].[bankholidays]( [id] [int] IDENTITY(1,1) NOT NULL, [holiday] [nvarchar](150) COLLATE Latin1_General_CI_AS NULL, [holidaydate] [datetime] NULL) ON [PRIMARY] I want to make a stored procedure that i pass a date and it if the date is not in the bankholidays list it returns that date otherwise it returns the next date not in the bankholidays list can someone help me with this? |
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2010-06-30 : 07:22:34
|
Try something like thisif exists(select * from bankholidays where holiday=@date) select min(dates) as next_date from ( select dateadd(day,number,@date) as dates from master..spt_values where type='p' and number>=0 ) as t1 left join bankholidays as t2 on t1.dates=t2.holiday where t2.holiday is nullelse select @date as next_date MadhivananFailing to plan is Planning to fail |
 |
|
vaibhavktiwari83
Aged Yak Warrior
843 Posts |
Posted - 2010-06-30 : 07:34:12
|
quote: Originally posted by madhivanan Try something like thisif exists(select * from bankholidays where holidaydate=@date) select min(dates) as next_date from ( select dateadd(day,number,@date) as dates from master..spt_values where type='p' and number>=0 ) as t1 left join bankholidays as t2 on t1.dates=t2.holidaydate where t2.holiday is nullelse select @date as next_date MadhivananFailing to plan is Planning to fail
little typo error in redVaibhav TTo walk FAST walk ALONE To walk FAR walk TOGETHER |
 |
|
esthera
Master Smack Fu Yak Hacker
1410 Posts |
Posted - 2010-06-30 : 07:36:50
|
thanks i'm getting Conversion failed when converting datetime from character string.and how can i add to this to also not allow it to return a weekend (then it also goes to the next day) |
 |
|
vaibhavktiwari83
Aged Yak Warrior
843 Posts |
Posted - 2010-06-30 : 07:42:21
|
quote: Originally posted by esthera thanks i'm getting Conversion failed when converting datetime from character string.
See my replyVaibhav TTo walk FAST walk ALONE To walk FAR walk TOGETHER |
 |
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2010-06-30 : 08:31:23
|
quote: Originally posted by vaibhavktiwari83
quote: Originally posted by madhivanan Try something like thisif exists(select * from bankholidays where holidaydate=@date) select min(dates) as next_date from ( select dateadd(day,number,@date) as dates from master..spt_values where type='p' and number>=0 ) as t1 left join bankholidays as t2 on t1.dates=t2.holidaydate where t2.holiday is nullelse select @date as next_date MadhivananFailing to plan is Planning to fail
little typo error in redVaibhav TTo walk FAST walk ALONE To walk FAR walk TOGETHER
Thanks MadhivananFailing to plan is Planning to fail |
 |
|
esthera
Master Smack Fu Yak Hacker
1410 Posts |
Posted - 2010-06-30 : 12:53:25
|
thansk - can you help me with how to also not let it fall on a weekend |
 |
|
X002548
Not Just a Number
15586 Posts |
Posted - 2010-06-30 : 14:35:23
|
I'm liking thisCREATE TABLE Holidays (Holiday_Dt datetime, HoldayName varchar(255))GOINSERT INTO Holidays(Holiday_Dt, HoldayName)SELECT '1/1/2010', 'New Years Day' UNION ALLSELECT '1/18/2010', 'Martin Luther King' UNION ALLSELECT '2/15/2010', 'President''s Day' UNION ALLSELECT '5/31/2010', 'Memorial Day' UNION ALLSELECT '7/5/2010', 'July 4th Holiday' UNION ALLSELECT '9/6/2010', 'Labor Day' UNION ALLSELECT '11/25/2010', 'Thanksgiving' UNION ALLSELECT '11/26/2010', 'Black Friday' UNION ALLSELECT '12/24/2010', 'Christmas Holiday' UNION ALLSELECT '12/31/2010', 'New Years Eve'GODECLARE @inp_Dt datetime; SET @inp_Dt = '11/25/2010' SELECT TOP 1 CASE WHEN Holiday_Dt IS NULL THEN inp_Dt ELSE DATEADD(d,n,inp_Dt) END AS Next_Available_Dt FROM (SELECT @inp_Dt AS inp_Dt) AS XXX LEFT JOIN Holidays h ON h.Holiday_Dt = xxx.inp_Dt LEFT JOIN (SELECT 1 n UNION SELECT 2 n UNION SELECT 3 n UNION SELECT 4 n UNION SELECT 5 n UNION SELECT 6 n UNION SELECT 7 n) AS n ON xxx.inp_Dt < DATEADD(d,n,inp_Dt) WHERE DATENAME(dw,DATEADD(d,n,inp_Dt)) NOT IN ('Saturday','Sunday') AND NOT EXISTS (SELECT * FROM Holidays WHERE Holiday_Dt = DATEADD(d,n,inp_Dt))GODROP TABLE HolidaysGO Brett8-)Hint: Want your questions answered fast? Follow the direction in this linkhttp://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspxAdd yourself!http://www.frappr.com/sqlteam |
 |
|
madhivanan
Premature Yak Congratulator
22864 Posts |
|
esthera
Master Smack Fu Yak Hacker
1410 Posts |
Posted - 2010-07-01 : 02:11:01
|
thanks - the only thing with this is if i put in a date on saturday it stays the same date |
 |
|
esthera
Master Smack Fu Yak Hacker
1410 Posts |
Posted - 2010-07-04 : 06:44:25
|
why does the following code not work with the dates?if exists(select * from bankholidays where holidaydate=@date) select min(dates) as next_date from ( select dateadd(day,number,@date) as dates from master..spt_values where type='p' and number>=0 ) as t1 left join bankholidays as t2 on t1.dates=t2.holidaydate where t2.holidaydate is null and DATEPART(WEEKDAY, @date)<>7 and DATEPART(WEEKDAY, @date)<>1else select @date as next_date |
 |
|
|