有没有办法得到一个布尔,而不在SQL Server中转换? [英] Is there a way to get a boolean without casting in SQL Server?

查看:225
本文介绍了有没有办法得到一个布尔,而不在SQL Server中转换?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现很奇怪,像这样的简单代码是无效的:

I've found it very weird that simple code like this is not valid:

select * from table where field=true

替代品显然是

select * from table where field='true'

我可以住在那里。由于某种原因,我最近需要这样做:

Ok, guess I can live with that. For one reason or another I needed to do something like this recently:

select true as somefield,...

获取类型和一切权利的替代方法是更加丑陋的:

The alternative to get types and everything right was much more ugly though:

select cast('true' as bit) as somefield,...

我错过了什么?是否真的有一些内置的方式来获得一个真或假的值作为一个布尔而不转换?

Am I missing something? Is there actually some built in way to get a true or false value as a boolean without casting?

推荐答案

Sql Server不有一个布尔数据类型,你可以存储在一个表中,但它包含一个可以存储的位数据类型。

Sql Server doesn't have a boolean datatype you can store in a table, but it does contain a bit data type you can store.

但是, true 关键字你看到的是一个布尔值。 Sql Server仅对比较运算符的结果使用布尔数据类型,它可以返回三个值,TRUE,FALSE和UNKNOWN。

However, the true keyword you see IS a boolean value. Sql Server only uses the boolean data type for results for its Comparison Operators, which can return three values, TRUE, FALSE, and UNKNOWN.

重要的是要知道两者之间的区别。

It is important to know the distinction between the two.

SELECT *
FROM Table
WHERE Field = 1 --Field = true

要在表中存储布尔值,请使用位数据类型。使用 1表示真实 0表示false Null表示未知

To store boolean values in a table, use the bit datatype. Use 1 for true, 0 for false, and Null for unknown.

这篇关于有没有办法得到一个布尔,而不在SQL Server中转换?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆