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 |
|
Sanjay_Bakshi
Starting Member
14 Posts |
Posted - 2008-07-18 : 15:37:07
|
| Dear All,I have one tableSession-DaysRequired-SessionStartDate1-2-07/14/2008total 100 session i havenow 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 2000Sanjay |
|
|
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] |
 |
|
|
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 tableSession-DaysRequired-SessionStartDate1-2-07/14/2008total 100 session i havenow 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 2000Sanjay
is this you wantcreate a udf like thisCREATE FUNCTION BusinessDayAdd(@DateValue datetime,@count int)RETURNS datetime ASBEGINDECLARE @ReturnDate datetimeSELECT @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 HolidayFROM master..spt_valuesWHERE type='p'AND number>0AND number<=@count)tRETURN @ReturnDateEND and use it in your query belowDECLARE @InitialSessionDate datetimeSELECT @InitialSessionDate=SessionDateFROM YourtableWHERE Session=1UPDATE ySET y.SessionStartDate=tmp.SessionStartDateFROM yourtable yINNER JOIN(SELECT Session,dbo.BusinessDayAdd(@InitialSessionDate,b.daycount)AS SessionStartDateFROM yourtable tCROSS APPLY (SELECT SUM(DaysRequired) AS daycount FROM yourtable t WHERE Session <t.Session) b)tmpON tmp.Session=y.Session |
 |
|
|
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] |
 |
|
|
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 ASBEGINDECLARE @ReturnDate datetimeSELECT @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 HolidayFROM master..spt_valuesWHERE type='p'AND number>0AND number<=@count)tRETURN @ReturnDateENDand use it in your query belowDECLARE @InitialSessionDate datetimeSELECT @InitialSessionDate=SessionDateFROM YourtableWHERE Session=1UPDATE ySET y.SessionStartDate=tmp.SessionStartDateFROM yourtable yINNER JOIN(SELECT t.Session,dbo.BusinessDayAdd(@InitialSessionDate,SUM(b.DaysRequired))AS SessionStartDateFROM yourtable tINNER JOIN yourtable bON b.Session <t.SessionWHERE t.Session>1GROUP BY t.Session)tmpON tmp.Session=y.Session |
 |
|
|
|
|
|
|
|