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

查看:105
本文介绍了从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)

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天全站免登陆