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)
 Datediff Case Statement

Author  Topic 

petek
Posting Yak Master

192 Posts

Posted - 2010-11-11 : 05:01:25
Hi i need to create a SQL statement to output a 1 or 0 depending on the value from the query.

the query i have is:

SELECT DATEDIFF(minute, ServerLastAccessDate, getdate()) AS Mins FROM timegate

what i need is if Mins is greater than 20 then return a 1 else 0

Is this possible?

i tried the case staement but am not having any joy...your help will be appreciated

cheers

Pete

Kind Regards

Pete.

Sachin.Nand

2937 Posts

Posted - 2010-11-11 : 05:04:08
[code]
SELECT case
when DATEDIFF(minute, ServerLastAccessDate, getdate()) >20 then 1 else 0
AS Mins FROM timegate
[/code]



PBUH

Go to Top of Page

petek
Posting Yak Master

192 Posts

Posted - 2010-11-11 : 05:10:29
hi Sachin....thanks for the reply but i get:

Msg 156, Level 15, State 1, Line 3
Incorrect syntax near the keyword 'AS'.


Kind Regards

Pete.
Go to Top of Page

petek
Posting Yak Master

192 Posts

Posted - 2010-11-11 : 05:18:08
Hi missing an END....


SELECT case when DATEDIFF(minute, ServerLastAccessDate, getdate()) >20 then 1 else 0 end as 'Mins' FROM timegate

Kind Regards

Pete.
Go to Top of Page

Sachin.Nand

2937 Posts

Posted - 2010-11-11 : 05:39:17
quote:
Originally posted by petek

Hi missing an END....


SELECT case when DATEDIFF(minute, ServerLastAccessDate, getdate()) >20 then 1 else 0 end as 'Mins' FROM timegate

Kind Regards

Pete.



Yup.I missed the END part.

PBUH

Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2010-11-11 : 06:36:12
quote:
Originally posted by petek

Hi missing an END....


SELECT case when DATEDIFF(minute, ServerLastAccessDate, getdate()) >20 then 1 else 0 end as 'Mins' FROM timegate

Kind Regards

Pete.


Also dont use single quotes around alias name

Madhivanan

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

- Advertisement -