Getting boolean from comparison date in t-sql select

I am wondering if something is possible in the following lines in ms-sql (2005)

SELECT (expiry & ltd getdate ()) AS Expired FROM MyTable WHERE (ID = 1)

Basically I want to evaluate a date comparable to a logical one, is this possible in the selected part of the instruction?

+3
source share
2 answers

Not directly. You must use CASE, CAST means that it is interpreted as boolean by client code

SELECT
    CAST(CASE WHEN expiry < getdate() THEN 1 ELSE 0 END AS bit) AS Expired
FROM
    MyTable WHERE (ID = 1)

Another solution where one or zero rows are expected:

SELECT
    CAST(COUNT(*) AS bit) AS Expired   
FROM
    MyTable
WHERE
    ID = 1 AND expiry < getdate() 
+8
source
SELECT CASE WHEN expiry < getdate() THEN 'true' ELSE 'false' END AS Expired FROM MyTable WHERE (ID = 1)
0
source

Source: https://habr.com/ru/post/1725544/


All Articles