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
 Expiry Date auto change field

Author  Topic 

mlawton40
Starting Member

15 Posts

Posted - 2008-02-02 : 10:46:53
Hi,

My table has more columns than this but I will just use the ones here tht are important.
I have two columns in a table these are "ExpiryDate" and "Archived".
When the row is created the "Archived" field will be null but the user will have entered a expiry date. When this date is reached I would like the "Archived" data to change to "1". Is this possible? Would I have to do it through some kind of script or can I set the database to do this automatically?

Thank you, Mark

slimt_slimt
Aged Yak Warrior

746 Posts

Posted - 2008-02-02 : 11:02:08
this might help:


create table t_dates
(id int not null,
archive datetime,
expiry datetime,
caseif char(1)
)

insert into t_dates (id, archive, expiry, caseif) values (1, getdate(), getdate()+3, 0)
insert into t_dates (id, archive, expiry, caseif) values (2, getdate(), getdate()-5, 0)
insert into t_dates (id, archive, expiry, caseif) values (3, getdate(), getdate()+12, 0)
insert into t_dates (id, archive, expiry, caseif) values (4, getdate(), getdate()+3, 0)


update t_dates
set caseif = 1
where archive >= expiry
Go to Top of Page

slimt_slimt
Aged Yak Warrior

746 Posts

Posted - 2008-02-02 : 11:07:38
or if you don't want to use caseif field, try this:



update t_dates

set archive =
case when (archive < expiry ) then archive
when (archive >= expiry) then expiry
end

Go to Top of Page

TG
Master Smack Fu Yak Hacker

6065 Posts

Posted - 2008-02-02 : 11:19:10
One way is to create a sql job, scheduled for (whenever) that executes this command:

update myTable set
Archived = 1
from myTable
where ExpiryDate <= getdate()
and Archived is NULL


Be One with the Optimizer
TG
Go to Top of Page
   

- Advertisement -