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 2000 Forums
 SQL Server Development (2000)
 Date Ranges

Author  Topic 

AskSQLTeam
Ask SQLTeam Question

0 Posts

Posted - 2001-12-19 : 09:01:25
pradeep writes "i Have a table with a primary key and 2 other fields startdate ,stopdate

when i enter a new row i have to check if -

1- any record exists which has its start date lesser than startdate and stop date greater
than start date but lesser than stop date ,in that case i should update the xisting
records stop date with the new start date

2- any record exists which has its start date greater than new startdate and lesser
than stopdate,in that case i should update the new records stop date with the
old records start date

3- any record which lies betwn the startdate and stop date ,in that case
1, set the stop date for the new record as the start date of the old record
2,create a new record with start date as old records stop date and stop date as the new stop date


can u please help me out with this

thanks"

Nazim
A custom title

1408 Posts

Posted - 2001-12-19 : 09:27:27
you can write a trigger on insert

something like this should help u


update table t set t.stopdate=i.startdate,
from inserted i
where t.primarykey=i.primarykey
and t.startdate < i.startdate and t.stopdate > i.startdate

update table t set t.startdate=t.stopdate,
from inserted i
where t.primarykey=i.primarykey
and t.startdate > i.startdate and t.stardate < i.startdate

update table t set t.stopdate=t.startdate,
from inserted i
where t.primarykey=i.primarykey
and t.startdate between i.startdate and i.stopdate
and t.stopdate between i.startdate and i.stopdate

HTH


-------------------------
"Success is when Preparedness meets Opportunity"
Go to Top of Page
   

- Advertisement -