views:

21

answers:

1

How to use conditional select query in SQL Server ?

I have a column in which I store date type variable. Now I should retrieve one value from that column and find the difference with the current_timestamp and if the difference is less than 10 mins I should a return 1 and if more than 10 mins I should return 0.

Is this possible ?

05-12-2010 13.58.30 - 05-12-2010 13.56.30

here the difference is 2 so it should return 1.

+4  A: 

You can use the CASE statement. Lets say the column is named 'record_timestamp':

SELECT
    CASE 
       WHEN DATEDIFF(minute, record_timestamp, GETDATE()) < 10 THEN 1 
       ELSE 0 
END
Darksider