Showing posts with label condition. Show all posts
Showing posts with label condition. Show all posts

Wednesday, March 21, 2012

Problem with a date function

Hello All!

I have a table with a date column. I would like to be able to DELETE the rows based on the date column. The condition is 30 days from todays date. So anything older than 30 days from todays date, it will delete those rows.

Any suggesttion the best way to do this. I was thinking of a simple select statement, but can't figure it out.

TIA!!

Rudy

Hi there,

Is your date column of data type DateTime? If so, try something like:

DELETE FROM [Table Name]
WHERE DATEADD(d, -30, GETDATE()) > [Date Column]

What happens is DATEADD(d, -30, GETDATE()) is used to obtain a date that is 30 days from todays date. Then, anything in the table where the entry in the Date Column (which I assume is of data type DateTime) is older than DATEADD(d, -30, GETDATE()), i.e. older than 30 days from today's date, gets deleted.

Hope that helps a bit, but sorry if it doesn't.
|||Thank you! Just what I needed!

Saturday, February 25, 2012

Problem Using if condition in select stored procesure

Hi!
how can i use if condition in where clause of select statement
I want the work like this
but this is not working correct
select * from Table1
where
if (@.a>1)
field1=@.a
else
field2=@.a
This is not a recommended usage since the optimizer will probably not
generate a good plan. It would be better to use a IF statement and two
select statements. In addition, I find the logic more than a bit strange.
In any event, below is an example
use Northwind
go
--select * from invoices
declare @.a int
/*set @.a = 14
select * from invoices
where ProductID = @.a
set @.a = 10249
select * from invoices
where OrderID = @.a*/
set @.a = 10249
set @.a = 14
select * from invoices
where case when @.a > 1000 then OrderID else @.a end = @.a
and case when @.a <= 1000 then ProductID else @.a end = @.a
"microsoft" <shahzad@.procuresoft.com> wrote in message
news:enf58S05EHA.2964@.TK2MSFTNGP09.phx.gbl...
> Hi!
> how can i use if condition in where clause of select statement
> I want the work like this
> but this is not working correct
> select * from Table1
> where
> if (@.a>1)
> field1=@.a
> else
> field2=@.a
>
|||select * from Table1 where @.a>1 and field1=@.a
Union
select * from Table1 where @.a<1 and field2=@.a
"microsoft" wrote:

> Hi!
> how can i use if condition in where clause of select statement
> I want the work like this
> but this is not working correct
> select * from Table1
> where
> if (@.a>1)
> field1=@.a
> else
> field2=@.a
>
>
|||where
(@.a > 1 and field1 = @.a) or (@.a <= 1 and field2 = @.a)
or (probably less efficient)
where
@.a = case when @.a > 1 then field1 else field2 end
If you have a non-ansi setting for null comparisons,
where
(@.a > 1 and field1 = @.a) or ((@.a <= 1 or @.a is null) and field2 = @.a)
Steve Kass
Drew University
microsoft wrote:

>Hi!
>how can i use if condition in where clause of select statement
>I want the work like this
>but this is not working correct
>select * from Table1
> where
> if (@.a>1)
> field1=@.a
> else
> field2=@.a
>
>