kellelein
kellelein

Reputation: 113

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.

Upvotes: 0

Views: 95

Answers (1)

Madhivanan
Madhivanan

Reputation: 13700

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)

Upvotes: 2

Related Questions