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)
 Date Comparison

Author  Topic 

ann
Posting Yak Master

220 Posts

Posted - 2008-10-04 : 16:05:18
I want to do a date comparison based on days, and if the days are greater than x amount I want to delete that record:

psuedo:

if tableA,column1 somedate >= 15 then delete record

I don't know how to do this, I know how to do a select based on a daterange but not sure how to go about this.

Can anyone help?
Thanks

sodeep
Master Smack Fu Yak Hacker

7174 Posts

Posted - 2008-10-04 : 16:17:21
Use DateDiff function.
Go to Top of Page

ann
Posting Yak Master

220 Posts

Posted - 2008-10-04 : 16:24:20
ok - so I can do the datediff to see the difference in days:

Select interval = DateDiff(day, DateAdded, GetDate())
From TableA


so how do I add the part that determines if the interval is greater than 15, then delete the record?
Go to Top of Page

TG
Master Smack Fu Yak Hacker

6065 Posts

Posted - 2008-10-04 : 18:56:06
delete
--select *
from tableA a
where datediff(day, a.<yourDateColumn>, getdate()) >= 15


run the blue part first to make sure you are about to delete only the intended rows. When you are satisfied then run the entire thing. (the SELECT part is commented out)

Be One with the Optimizer
TG
Go to Top of Page

ann
Posting Yak Master

220 Posts

Posted - 2008-10-04 : 19:03:41
THANK YOU!!

I had crazy stuff I was trying with if statements, etc., - so glad you replied

Thanks again
Go to Top of Page
   

- Advertisement -