Link to home
Start Free TrialLog in
Avatar of srikotesh
srikotesh

asked on

if my query returns empty i want to display input as output

Hi Expets,

query:
=======
select ci_id from category where parent_id =236282
UNION
select ci_id from category where parent_id in(select category_id from
category where parent_id=236282)

the above query some times it will returns empty
i want to display 236282 as the output if my query returns empty

can some one help me how to do it.
Avatar of Raheman M. Abdul
Raheman M. Abdul
Flag of United Kingdom of Great Britain and Northern Ireland image

Try this: (not tested)
IF NOT EXISTS (select ci_id from category where parent_id =236282
UNION
select ci_id from category where parent_id in(select category_id from
category where parent_id=236282)
)
BEGIN 
  SELECT 236282
END
ELSE 
BEGIN
(select ci_id from category where parent_id =236282
UNION
select ci_id from category where parent_id in(select category_id from
category where parent_id=236282)
)
END

Open in new window

Try
select isnull(ltrim(ci_id),'236282') from
(select ci_id from category where parent_id =236282
UNION
select ci_id from category where parent_id in(select category_id from
category where parent_id=236282)) a

Open in new window

Avatar of srikotesh
srikotesh

ASKER

hi HuaMinChen,

the above query is not working
ASKER CERTIFIED SOLUTION
Avatar of skullnobrains
skullnobrains

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I got the solution for this query.

SELECT      * FROM      category WHERE      parent_id = 236282       OR parent_id IN (                   SELECT      category_id                   FROM      category                   WHERE       parent_id = 236282             );
which lists all children and grandchildren of the given id (same as the query you posted originally)

not sure about this : "i want to display 236282 as the output if my query returns empty" or what you actually wanted in the first place

good for you if you got it working

ps : you can accept your own answer if you don't want to spend your points
Hi skullnobrains,

i am getting the expected result from the second query as u posted

THANKS