来自select的case为null

时间:2012-02-22 18:05:53

标签: sql sql-server tsql

我有很多数据,为了示例目的,请提供以下数据

    d1  date                        OL
    N   2012-03-09 00:00:00.000     NULL
    No  2011-09-26 00:00:00.000     OL
    N   2012-01-26 00:00:00.000     NOL
    N   2012-03-07 00:00:00.000     NOL
    yes 2012-02-23 00:00:00.000     NULL

我需要像是

Create proc dbo.usp_all_property_search
(@type varchar(2))
as
begin
select * from tbl where ol is
select case @type
when 'o' then null
else
not null
end

end

Plz帮帮我。

5 个答案:

答案 0 :(得分:2)

你过度复杂了:

SELECT *
FROM Tbl
WHERE (@type = 'o' AND ol IS NULL)
OR (@Type <> 'o' AND ol IS NOT NULL)

答案 1 :(得分:1)

select * from tbl
where (@type = 'o' and ol is null) OR
    (@type <> 'o' and ol is not null)

答案 2 :(得分:0)

我想你想要这个:

select * from tbl where (@type = 'o' and ol is null) or (@type != 'o' and ol is not null)

Case 表达式必须返回,而SQL Server甚至不支持布尔数据类型。

答案 3 :(得分:0)

这有用吗?

Create proc dbo.usp_all_property_search
(@type varchar(2))
as
begin
select * from tbl 
where (@type = 'o' and ol is null)
OR (@type != 'o' AND ol is not null)

end

答案 4 :(得分:0)

Create proc dbo.usp_all_property_search(@type varchar(2))
as
begin
  select * 
  from tbl 
  where 
     ( @type = 'o' and
       ol is null)        or
     ( @type != 'o' and
       ol is not null);
end