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
 SQL Server 2005 Forums
 Transact-SQL (2005)
 return next available date

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 this

if 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 null

else
select @date as next_date


Madhivanan

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

vaibhavktiwari83
Aged Yak Warrior

843 Posts

Posted - 2010-06-30 : 07:34:12
quote:
Originally posted by madhivanan

Try something like this

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.holiday is null

else
select @date as next_date


Madhivanan

Failing to plan is Planning to fail



little typo error in red

Vaibhav T

To walk FAST walk ALONE
To walk FAR walk TOGETHER
Go to Top of Page

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)
Go to Top of Page

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 reply

Vaibhav T

To walk FAST walk ALONE
To walk FAR walk TOGETHER
Go to Top of Page

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 this

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.holiday is null

else
select @date as next_date


Madhivanan

Failing to plan is Planning to fail



little typo error in red

Vaibhav T

To walk FAST walk ALONE
To walk FAR walk TOGETHER


Thanks

Madhivanan

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

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
Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2010-06-30 : 14:35:23
I'm liking this


CREATE TABLE Holidays (Holiday_Dt datetime, HoldayName varchar(255))
GO

INSERT INTO Holidays(Holiday_Dt, HoldayName)
SELECT '1/1/2010', 'New Years Day' UNION ALL
SELECT '1/18/2010', 'Martin Luther King' UNION ALL
SELECT '2/15/2010', 'President''s Day' UNION ALL
SELECT '5/31/2010', 'Memorial Day' UNION ALL
SELECT '7/5/2010', 'July 4th Holiday' UNION ALL
SELECT '9/6/2010', 'Labor Day' UNION ALL
SELECT '11/25/2010', 'Thanksgiving' UNION ALL
SELECT '11/26/2010', 'Black Friday' UNION ALL
SELECT '12/24/2010', 'Christmas Holiday' UNION ALL
SELECT '12/31/2010', 'New Years Eve'
GO

DECLARE @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))
GO

DROP TABLE Holidays
GO




Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx

Add yourself!
http://www.frappr.com/sqlteam



Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2010-07-01 : 01:59:19
<<
SET @inp_Dt = '11/25/2010'
>>

Always use unambiguous format YYYYMMDD
Refer this for more informations
http://beyondrelational.com/blogs/madhivanan/archive/2010/06/03/understanding-datetime-column-part-ii.aspx

Madhivanan

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

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
Go to Top of Page

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)<>1

else
select @date as next_date
Go to Top of Page
   

- Advertisement -