简体   繁体   中英

Select inside an if else or case sql expression

I would like to make a select expression inside a if else or case expression, but I can't get it to work.

I have tried the folowing

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

and this

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

None of them worked and I just can't figure out, what the problem is.

One of the ways is

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)

or

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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