如何在T-SQL中根据列的内容输出布尔值?

发布于 2024-07-07 02:01:00 字数 203 浏览 6 评论 0原文

我对不同表的抽象列进行了视图并对它们进行预过滤和预排序。 有一列的内容我不关心,但我需要知道内容是否为空。 因此,我的视图应该将别名传递为“true”,以防指定列的值不为 null,并传递“false”以防万一值为空

如何使用 T-SQL 选择这样的布尔值?

I made a view to abstract columns of different tables and pre-filter and pre-sort them. There is one column whose content I don't care about but I need to know whether the content is null or not. So my view should pass an alias as "true" in case the value of this specified column isn't null and "false" in case the value is null.

How can I select such a boolean with T-SQL?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

安静 2024-07-14 02:01:00

为此,您必须使用 CASE 语句:

SELECT CASE WHEN columnName IS NULL THEN 'false' ELSE 'true' END FROM tableName;

You have to use a CASE statement for this:

SELECT CASE WHEN columnName IS NULL THEN 'false' ELSE 'true' END FROM tableName;
白首有我共你 2024-07-14 02:01:00

或者你可以这样做:

    SELECT RealColumn, CAST(0 AS bit) AS FakeBitColumn FROM tblTable

Or you can do like this:

    SELECT RealColumn, CAST(0 AS bit) AS FakeBitColumn FROM tblTable
楠木可依 2024-07-14 02:01:00

如果您需要布尔值输出

CAST(CASE WHEN colName IS NULL THEN 0  ELSE 1   END as BIT) aIsBooked

If you need a output as boolean

CAST(CASE WHEN colName IS NULL THEN 0  ELSE 1   END as BIT) aIsBooked
坐在坟头思考人生 2024-07-14 02:01:00

对于视图中的列,您可以

CASE WHEN ColumnName is not null THEN 'True' ELSE 'False' END

在声明中使用类似 或 的

SELECT 
s.ID,
s.[Name],
CASE WHEN s.AchievedDate is not null THEN 'True' ELSE 'False' END [IsAchieved]
FROM Schools s

内容,或者在事后进行进一步处理,我个人会使用

SELECT 
s.ID,
s.[Name],
CASE WHEN s.AchievedDate is not null THEN 1 ELSE 0 END [IsAchieved]
FROM Schools s

for the column in the view you can use something like

CASE WHEN ColumnName is not null THEN 'True' ELSE 'False' END

or in a statement

SELECT 
s.ID,
s.[Name],
CASE WHEN s.AchievedDate is not null THEN 'True' ELSE 'False' END [IsAchieved]
FROM Schools s

or for further processing afterwards I would personally use

SELECT 
s.ID,
s.[Name],
CASE WHEN s.AchievedDate is not null THEN 1 ELSE 0 END [IsAchieved]
FROM Schools s
双手揣兜 2024-07-14 02:01:00

我遇到了类似的问题,我希望视图根据实际列是否为空来返回布尔列类型。 我创建了一个用户定义的函数,如下所示:

CREATE FUNCTION IsDatePopulated(@DateColumn as datetime)
RETURNS bit
AS
BEGIN
    DECLARE @ReturnBit bit;

    SELECT @ReturnBit = 
        CASE WHEN @DateColumn IS NULL 
            THEN 0 
            ELSE 1 
        END

    RETURN @ReturnBit
END

然后我创建的视图返回一个位列,而不是一个整数。

CREATE VIEW testView
AS
    SELECT dbo.IsDatePopulated(DateDeleted) as [IsDeleted] 
    FROM Company

I had a similar issue where I wanted a view to return a boolean column type based on if an actual column as null or not. I created a user defined function like so:

CREATE FUNCTION IsDatePopulated(@DateColumn as datetime)
RETURNS bit
AS
BEGIN
    DECLARE @ReturnBit bit;

    SELECT @ReturnBit = 
        CASE WHEN @DateColumn IS NULL 
            THEN 0 
            ELSE 1 
        END

    RETURN @ReturnBit
END

Then the view that I created returns a bit column, instead of an integer.

CREATE VIEW testView
AS
    SELECT dbo.IsDatePopulated(DateDeleted) as [IsDeleted] 
    FROM Company
樱花落人离去 2024-07-14 02:01:00

您要求布尔值,我们在 t-sql 中将其称为位。

其他答案要么给你一个varchar“true”和“false”,要么给你1和0。“true”和“false”显然是varchar,而不是布尔值。 我相信 1 和 0 会被转换为整数,但它肯定不是一个位。 这可能看起来很挑剔,但类型通常很重要。

要获得实际的位值,您需要将输出显式转换为类似于:

select case when tableName.columnName IS NULL then cast(0 as bit) else cast(1
as bit) END as ColumnLabel from tableName

You asked for boolean, which we call bit in t-sql.

Other answers have either given you a varchar 'true' and 'false' or 1 and 0. 'true' and 'false' are obviously varchar, not boolean. I believe 1 and 0 would be cast as an integer, but it's certainly not a bit. This may seem nit-picky, but types matter quite often.

To get an actual bit value, you need to cast your output explicitly as a bit like:

select case when tableName.columnName IS NULL then cast(0 as bit) else cast(1
as bit) END as ColumnLabel from tableName
末骤雨初歇 2024-07-14 02:01:00

我认为这比其他解决方案稍微简单一些:

SELECT Cast(ISNULL([column name], 0) AS BIT) AS IsWhatever

I think this is slightly simpler then the other solutions:

SELECT Cast(ISNULL([column name], 0) AS BIT) AS IsWhatever

等往事风中吹 2024-07-14 02:01:00

从 SQL Server 2012 开始,您可以使用 IIF

IIF(columnName IS NULL, 'false', 'true')

Since SQL server 2012 you can use IIF

IIF(columnName IS NULL, 'false', 'true')
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文