我正在使用SQL Server Management Studio 2014并尝试拉出查询以包括自然二进制的所有位列(0或1),并添加一列以指示这些列的总和是否大于1。 问题是所有列都是位数据类型,我需要将它们强制转换为int以便进行任何加法运算。我想知道是否有一种更简便的方法来CAST多列,而不是为每列重写CAST函数。
例如,如果这些列全部为位:
student, graduated, incomplete
SELECT CASE
WHEN CAST(student AS INT) + CAST(graduated AS INT) + CAST(incomplete AS INT) > 0
THEN 'complete' END AS status_shown
FROM table1
答案 0 :(得分:2)
您根本不需要投射:
select (case when student = 1 or graduated = 1 or incomplete = 1
then 'complete'
end) as status_shown
from table1;
答案 1 :(得分:0)
如果列是BIT,则可以:
SELECT CASE 1 WHEN t.student|t.graduated|t.incomplete THEN 'complete' END AS [status_shown]
FROM table1 t;