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
 update date

Author  Topic 

Sanjay_Bakshi
Starting Member

14 Posts

Posted - 2008-07-18 : 15:37:07
Dear All,
I have one table
Session-DaysRequired-SessionStartDate
1-2-07/14/2008
total 100 session i have

now i have to update Sesison Start Date in rest 99 line based on session start date with dateadd(dd,DaysRequired,SessionStartDate)
if value is Sunday then add 1 if value is Saturday then add2.

How to do this thing in SQL 2000



Sanjay

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2008-07-18 : 21:32:52
you want to create the rest of the records or update ?

What are the value for DaysRequired for the rest of the Session ?



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

Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-07-19 : 01:22:45
quote:
Originally posted by Sanjay_Bakshi

Dear All,
I have one table
Session-DaysRequired-SessionStartDate
1-2-07/14/2008
total 100 session i have

now i have to update Sesison Start Date in rest 99 line based on session start date with dateadd(dd,DaysRequired,SessionStartDate)
if value is Sunday then add 1 if value is Saturday then add2.

How to do this thing in SQL 2000



Sanjay


is this you want

create a udf like this

CREATE FUNCTION BusinessDayAdd
(
@DateValue datetime,
@count int
)
RETURNS datetime
AS
BEGIN
DECLARE @ReturnDate datetime
SELECT @ReturnDate=DATEADD(d,Holiday,MAX(DateValue))
FROM
(SELECT DATEADD(d,number,@DateValue) AS DateValue,
CASE WHEN DATENAME(dw,DATEADD(d,number,@DateValue))='Sunday'
OR DATENAME(dw,DATEADD(d,number,@DateValue))='Saturday'
THEN 1 ELSE 0 END AS Holiday
FROM master..spt_values
WHERE type='p'
AND number>0
AND number<=@count)t

RETURN @ReturnDate
END




and use it in your query below

DECLARE @InitialSessionDate datetime
SELECT @InitialSessionDate=SessionDate
FROM Yourtable
WHERE Session=1

UPDATE y
SET y.SessionStartDate=tmp.SessionStartDate
FROM yourtable y
INNER JOIN
(
SELECT Session,dbo.BusinessDayAdd(@InitialSessionDate,b.daycount)AS SessionStartDate
FROM yourtable t
CROSS APPLY (SELECT SUM(DaysRequired) AS daycount
FROM yourtable t
WHERE Session <t.Session) b
)tmp
ON tmp.Session=y.Session
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2008-07-19 : 01:37:18
Visakh, Sanjay is using SQL 2000


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

Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-07-19 : 01:54:12
quote:
Originally posted by khtan

Visakh, Sanjay is using SQL 2000


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




Ah i missed that part. Thanks for pointing out Khtan

then modify like this:-

CREATE FUNCTION BusinessDayAdd
(
@DateValue datetime,
@count int
)
RETURNS datetime
AS
BEGIN
DECLARE @ReturnDate datetime
SELECT @ReturnDate=DATEADD(d,SUM(Holiday),MAX(DateValue))
FROM
(SELECT DATEADD(d,number,@DateValue) AS DateValue,
CASE WHEN DATENAME(dw,DATEADD(d,number,@DateValue))='Sunday'
OR DATENAME(dw,DATEADD(d,number,@DateValue))='Saturday'
THEN 1 ELSE 0 END AS Holiday
FROM master..spt_values
WHERE type='p'
AND number>0
AND number<=@count)t

RETURN @ReturnDate
END




and use it in your query below

DECLARE @InitialSessionDate datetime
SELECT @InitialSessionDate=SessionDate
FROM Yourtable
WHERE Session=1

UPDATE y
SET y.SessionStartDate=tmp.SessionStartDate
FROM yourtable y
INNER JOIN
(
SELECT t.Session,dbo.BusinessDayAdd(@InitialSessionDate,SUM(b.DaysRequired))AS SessionStartDate
FROM yourtable t
INNER JOIN yourtable b
ON b.Session <t.Session
WHERE t.Session>1
GROUP BY t.Session
)tmp
ON tmp.Session=y.Session


Go to Top of Page
   

- Advertisement -