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)
 I can't figure out the systax for this statement

Author  Topic 

johnstern
Yak Posting Veteran

67 Posts

Posted - 2007-06-20 : 11:08:51
I am trying to get a 0 or 1 value ( false/true) if a date is " younger" than 7 days

here is my pseudo code (startDate is the column name)

Select
if (startDate >= GetDate() – 7)
new = 1
else
new = 0
As NewRec
From mytable


what would be the correct syntax to accomplish this


harsh_athalye
Master Smack Fu Yak Hacker

5581 Posts

Posted - 2007-06-20 : 11:15:51
You need to use CASE statement for this.

Select 
case When startDate >= DateAdd(day, -7, GetDate()) then 1
else 0 end As NewRec
From mytable




Harsh Athalye
India.
"The IMPOSSIBLE is often UNTRIED"
Go to Top of Page

charlie.snell
Starting Member

3 Posts

Posted - 2007-06-20 : 11:22:18
SELECT
CASE
WHEN (@STARTDATE >= (GETDATE()-7)) THEN 1
ELSE 0
END
Go to Top of Page

charlie.snell
Starting Member

3 Posts

Posted - 2007-06-20 : 11:26:33
Is using DateAdd better / more standardised than the (getdate()-7) snippet in my sql snippet?
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2007-06-21 : 10:12:35
Set the execution plan and see

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -