如何在 SQL 中设置布尔值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/41695064/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 05:05:53  来源:igfitidea点击:

How to set bool value in SQL

sqlsql-server

提问by jason

I have a column which is bool. How can I set true, false values for that? Here is my query :

我有一个布尔列。如何为此设置真、假值?这是我的查询:

Update [mydb].[dbo].[myTable]
SET isTrue =
(
CASE WHEN Name = 'Jason' THEN 1
END
)

I don't know what to write after THENkeyword. Should I write 1or trueor 1 AS BITor something else?

我不知道在THEN关键字后写什么。我应该写1true还是1 AS BIT或其他什么?

回答by Zohar Peled

Sql server does not have a booleandata type.
Instead, it has a bitdata type where the possible values are 0or 1.
So to answer your question, you should use 1to indicate a truevalue, 0to indicate a falsevalue, or nullto indicate an unknown value.

Sql server 没有boolean数据类型。
相反,它具有一种bit数据类型,其中可能的值为0or 1
因此,要回答您的问题,您应该使用1来表示true值、0表示false值或null表示未知值。

Update [mydb].[dbo].[myTable]
SET isTrue =
CASE WHEN Name = 'Jason' THEN 
    1
ELSE 
    0
END

回答by K D

The query you added will work fine, but it will you have to take care of "FALSE" part as well otherwise it will try to enter NULL in your column. Do you have any default value constrain on isTrue column?

您添加的查询可以正常工作,但您还必须处理“FALSE”部分,否则它会尝试在您的列中输入 NULL。您对 isTrue 列有任何默认值约束吗?

Update [mydb].[dbo].[myTable]
SET isTrue =
(
   CASE WHEN Name = 'Jason' THEN 1 ELSE 0
END
)

回答by jainvikram444

You need case statement with when and else if not any condition satisfied

如果不满足任何条件,则需要带有 when 和 else 的 case 语句

Update [mydb].[dbo].[myTable]
SET isTrue = ( CASE WHEN Name = 'Jason' 
                   THEN 1 else 0 
               END)

回答by Miky

Use IIF function of sql server

使用sql server的IIF函数

DECLARE @a int = 45, @b int = 40;  
DECLARE @a int = 45, @b int = 40;  
SELECT IIF ( @a > @b, 'TRUE', 'FALSE' ) AS Result; 

Result  
--------  
TRUE  

(1 row(s) affected)

For Your Problem

为您解决问题

Update [mydb].[dbo].[myTable]
SET isTrue = ( Name = 'Jason', 'TRUE', 'FALSE' )