选择具有默认值的可为空位

发布于 2024-09-11 20:01:49 字数 271 浏览 2 评论 0原文

我需要在视图中选择一个可为空的位列,但只要值为 NULL,就使用默认值 FALSE。 (由于其他原因,我无法在源表本身上添加默认值。)这就是我正在做的事情。

CAST 
(
    CASE 
    WHEN bit_column IS NULL THEN 0 
    ELSE bit_column  
END 
    AS BIT
) AS bit_column,
...

我必须在四列上执行此操作,所以我想知道是否有更好/更有效的方法来执行此操作。

I need to select a nullable bit column in a view, but use a default value of FALSE whenever the value is NULL. (For other reasons, I can't add the default value on the source table itself.) Here is what I am doing.

CAST 
(
    CASE 
    WHEN bit_column IS NULL THEN 0 
    ELSE bit_column  
END 
    AS BIT
) AS bit_column,
...

I have to do this on four columns, so I'm wondering if there is a better/more efficient way to do this.

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

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

发布评论

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

评论(4

执手闯天涯 2024-09-18 20:01:49

使用 isnull 函数。

isnull(bit_column, 0)

use the isnull function.

isnull(bit_column, 0)
浸婚纱 2024-09-18 20:01:49
SELECT coalesce(bit_column,0) bit_column
SELECT coalesce(bit_column,0) bit_column
花开半夏魅人心 2024-09-18 20:01:49

看看合并

Take a look at Coalesce

尘曦 2024-09-18 20:01:49

对于 T-SQL 使用

SELECT coalesce(bit_column, cast(0 as bit)) bit_column

在代码示例中,

 , examStatus.text
 , COALESCE(examStatus.archived, cast(0 as bit))

如果 examStatus.archived 为 NULL,则默认为 0(也称为 false)

For T-SQL use

SELECT coalesce(bit_column, cast(0 as bit)) bit_column

In a code example,

 , examStatus.text
 , COALESCE(examStatus.archived, cast(0 as bit))

If examStatus.archived is NULL it will default to 0 (aka false)

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