where子句和case语句在select语句中给出空记录

时间:2012-12-04 06:10:45

标签: sql-server-2008

我有一张名为Tb_patientBeds的表。

现在我想根据此表中的status列检索设置为已占用未占用或全部的记录。

以下是我的其他专栏:

patientBedID int IDENTITY(1,1) NOT NULL,
patientBedType [varchar](20) NULL,
BedCharge [varchar](20) NULL,
status [varchar](20) NULL,

我写了像

这样的查询
select * from Tb_patientBeds where [status]= case
when [status]= '0'
then 'occupied'
when [status]='1'
then 'unoccupied' else 'All'
end

查询没有返回记录,它显示空记录。

在这方面有人可以帮助我吗?

1 个答案:

答案 0 :(得分:1)

试试这个:

SELECT
    CASE 
    WHEN [status] = 1 THEN
        'unoccupied'
    WHEN [status] = 0 THEN
        'occupied'
    ELSE
        'All'
    END,
    *
FROM Tb_patientBeds
相关问题