从 t-sql 选择中的日期比较中获取布尔值 [英] Getting a boolean from a date compare in t-sql select

查看:38
本文介绍了从 t-sql 选择中的日期比较中获取布尔值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道在 ms-sql (2005) 中是否可以实现以下内容

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

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

我基本上想将日期与布尔值进行比较,这可能在语句的选择部分吗?

I basically want to evaluate the date compare to a boolean, is that possible in the select part of the statement?

推荐答案

不直接.您必须使用 CASE,CAST 意味着它被客户端代码解释为布尔值

Not directly. You have to use CASE, the CAST means it's 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() 

这篇关于从 t-sql 选择中的日期比较中获取布尔值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆