在if else或case sql表达式中选择

时间:2014-04-02 11:13:53

标签: sql select if-statement case

我想在if else或case表达式中创建一个select表达式,但我无法使它工作。

我尝试了以下

select
(case when DateTime1 is null then select * from LogEntries where PC_TID > '2014-03-26'
else select * from LogEntries where DateTime1 > '2014-03-26' 
end) as timeSearchParam
from LogEntries

和这个

if DateTime1 is null
begin
select * from LogEntries where PC_TID > '2014-03-26' 
end
else
begin
select * from LogEntries where DateTime1 > '2014-03-26' 
end

他们都没有工作,我只是想不通,问题是什么。

1 个答案:

答案 0 :(得分:2)

其中一种方法是

select * from LogEntries where PC_TID > '2014-03-26'  and DateTime1 is null
union all
select * from LogEntries where DateTime1 > '2014-03-26'  and DateTime1 is not null
order by coalesce(DateTime1 , PC_TID)

select * from LogEntries 
where 
(PC_TID > '2014-03-26'  and DateTime1 is null)
or
(DateTime1 > '2014-03-26'  and DateTime1 is not null)
order by coalesce(DateTime1 , PC_TID)