简体   繁体   English

如果列中存在值,则SQL Server查询返回1,否则返回0

[英]SQL Server query to return 1 if value exist in a column else return 0

I am trying to query the database for checking if a specific column has a value or not. 我正在尝试查询数据库以检查特定列是否具有值。 If there is a value in that column, the query should return 1, else it should return 0. 如果该列中有一个值,则查询应返回1,否则应返回0。

But my query is returning the total count of the columns for (ex:10). 但是我的查询返回的列总数为(ex:10)。

Note: query is done in Dell Boomi integration platform, SQL Server. 注意:查询是在Dell Boomi集成平台SQL Server中完成的。

select count (*) 
from ApplicationRequest 
where EmpID  = '993685' and ApplID = '1';

Do you just want case ? 你只想要case吗?

select (case when count(*) > 0 then 1 else 0 end)
from ApplicationRequest
where EmpID = 993685 and ApplID = 1;

I removed the single quotes around the comparisons. 我删除了比较中的单引号。 If they are really numbers then single quotes are not appropriate. 如果它们确实是数字,则不适合使用单引号。 If they are indeed strings, then use the single quotes. 如果它们确实是字符串,则使用单引号。

If this is what you want, a more efficient method would use exists : 如果这是你想要的,更有效的方法将使用exists

select (case when exists (select 1 
                          from ApplicationRequest
                          where EmpID = 993685 and ApplID = 1
                         )
             then 1 else 0
        end)

The aggregation query needs to find all matching rows. 聚合查询需要找到所有匹配的行。 This version can stop at the first one. 此版本可以在第一个版本停止。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM