Arya
Arya

Reputation: 8985

Return true/false based on elapsed time from SQL

Let's say we have an SQL query such as below

select now()-insert_time AS time_elapsed, *
from account
where enabled = true AND machine = 'one'
limit 1

is it possible to return a boolean column for now()-insert_time AS time_elapsed? I want it to be true if it's more than 4 hours, and false if it's less than 4 hours.

Upvotes: 1

Views: 216

Answers (1)

Mabu Kloesen
Mabu Kloesen

Reputation: 1358

You can compare it directly in your query

 select now()-insert_time AS time_elapsed,
        now()-insert_time >= '4 hours' as status,
        * 
   from account 
  where enabled = true 

In generally, you put it in CASE expression for other value output

 select now()-insert_time AS time_elapsed,
        CASE 
             WHEN now()-insert_time >= '4 hours' THEN 'Greater than 4 hours'
             ELSE 'Less than 4 hours'
        END as status,
        * 
   from account 
  where enabled = true 
        AND machine = 'one'  
  limit 1

Hopefully it will help you.

Upvotes: 3

Related Questions