Avatar of dpicco
dpicco
Flag for United States of America asked on

SQL query returning null

Hi experts,

This query should return 'No value' but it returns nothing. Anyone know why? Thanks.

SELECT colA=case when updt is null then 'No value' else updt end 
FROM         (SELECT  top 1 updt
                        FROM  dsr_journal
                        where status = 'Team Ended'
                        and updt = '8/6/2011'
                        ORDER BY updt DESC) a

Open in new window

Microsoft SQL ServerMicrosoft SQL Server 2005Microsoft SQL Server 2008SQL

Avatar of undefined
Last Comment
dpicco

8/22/2022 - Mon
Ephraim Wangoya

check what the subquery returns by itself. It may not be returning any records

SELECT  top 1 updt
                        FROM  dsr_journal
                        where status = 'Team Ended'
                        and updt = '8/6/2011'
                        ORDER BY updt DESC
dpicco

ASKER
that's right. the subquery returns nothing.

How can I get the main query to return 'No value' when the subquery returns nothing?

Thanks.
ASKER CERTIFIED SOLUTION
Patrick Matthews

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
Member_2_861731

Try this:

IF EXISTS(SELECT  1 FROM  dsr_journal where status = 'Team Ended' and updt = '8/6/2011')
SELECT colA=COALESCE(updt,'No value')
FROM         (SELECT  top 1 updt
                        FROM  dsr_journal
                        where status = 'Team Ended'
                        and updt = '8/6/2011'
                        ORDER BY updt DESC) a
ELSE
SELECT 'No value'
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
jorgedeoliveiraborges

Are you receiving a messagem:  (0 row(s) affected) ?
dpicco

ASKER
thank you