如何在选择子句中执行布尔运算 [英] How to do a boolean operation in a select clause

查看:77
本文介绍了如何在选择子句中执行布尔运算的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,

我想做一个选择语句,它返回一个布尔值

例如

 选择名字,姓氏(收入>  30000 )         

我该如何在语法上用mssql做到这一点?如果我按上述方法进行操作,则会收到语法错误

解决方案

,您可以使用case statement.

试试这个查询..

 选择名字,姓氏((大小写  30000  '  true'  else  ' span>  false' 结束) as 富人来自 tbl_person 



这将返回truefalse作为varchar.如果要在布尔值中分别替换为1和0.

 选择名,姓((大小写  30000   1  其他  0  结束) as  richperson 来自 tbl_person 


希望对您有所帮助.


尝试:

  SELECT 名字,姓氏,何时收入>  30000   THEN   1   0   END   AS  richperson  FROM  tbl_person 


首先,SQL Server没有布尔数据类型.最接近的等效项是bit.因此,这意味着根据您的需要,您可能必须在使用代码之前翻译"代码中的返回值.

Karthik Harve和OriginalGriff提供的SQL语句非常有效,但是要抛出另一个变体,您还可以执行以下操作:

  SELECT  p.firstname,
       p.lastname,
       CAST(p.income/30001  AS   bit ) AS 富人
 FROM  tbl_person p 


Hallo,

I want to do a select statement which returns a boolean value

e.g.

select firstname, lastname, (income > 30000) as richperson from tbl_person



how can I syntactically do this with mssql? If i do it like shown above I get a syntax error

解决方案

you can do with case statement.

try this query..

select firstname, lastname, (case when income > 30000 then 'true' else 'false' end) as richperson from tbl_person



this will return true or false as varchar. if you want in boolean replace with 1 and 0 respectively.

select firstname, lastname, (case when income > 30000 then 1 else 0 end) as richperson from tbl_person


hope it helps..


Try:

SELECT firstname, lastname, CASE WHEN income > 30000 THEN 1 ELSE 0 END AS richperson FROM tbl_person


First of all, SQL Server does not have a boolean data type. The closest equivalent is a bit. So this means that depending on your needs, you may have to ''translate'' the return value in your code before you use it.

The SQL statements provided by Karthik Harve and OriginalGriff are perfectly valid, but to throw another variant, you could also do something like:

SELECT p.firstname,
       p.lastname,
       CAST(p.income/30001 AS bit) AS richperson
FROM tbl_person p


这篇关于如何在选择子句中执行布尔运算的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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