如何在 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
How to set bool value in SQL
提问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关键字后写什么。我应该写1或true还是1 AS BIT或其他什么?
回答by Zohar Peled
Sql server does not have a boolean
data type.
Instead, it has a bit
data type where the possible values are 0
or 1
.
So to answer your question, you should use 1
to indicate a true
value, 0
to indicate a false
value, or null
to indicate an unknown value.
Sql server 没有boolean
数据类型。
相反,它具有一种bit
数据类型,其中可能的值为0
or 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' )