Thursday, March 22, 2012

If-Then in a select statement ?

I'm pulling a recordset for the last 8 weeks of data, but need to add a true/false flag in the recordset to indicate if the record is within the last 12 hours.

tblData.StartTime is the column that has the time for the record. I'm familiar with datediff() - but how do I check the value returned from that function to get a true/false (or 1/0) returned as a column in the select statement.

Select *, [What do I put here] FROM tblData

Thanks for your help,

AlexDECLARE @.myDateCol datetime
SELECT @.myDateCol = '1/19/2005'
SELECT CASE WHEN DateDiff(hh,@.myDateCol,GetDate()) < 12 THEN 1 ELSE 0 END AS TwelveHour_Ind|||I need to use this in a view...which doesn't support the case statement.

Any other way you can think of?

Thanks,

Alex|||Case statements are only not supported by Enterprise Manager. If you use Query Analyzer, you will be able to use Case statements.|||Use CASE for clarity, but if you like, here is an alternative method in the spirit of that famous Computer Scientist, Dr. Rube Goldberg:

declare @.TestDate datetime
set @.TestDate = '2005-01-19 14:39:29.530'
SET @.TestDate = dateadd(hh, -12, getdate())

select cast(sign(DateDiff(hh, GetDate(), @.TestDate) + 12) + 1 as bit)|||Open EM, and use it for refernece purposes...

NEVER Develop in EM, use QA.

CREATE VIEW myView99 AS
SELECT CASE WHEN DateDiff(hh,@.myDateCol,GetDate()) < 12 THEN 1 ELSE 0 END AS TwelveHour_Ind
, Col2
, Col3
FROM myTable99

No comments:

Post a Comment