Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 NULL Row then result

Author  Topic 

jmarkel
Starting Member

1 Post

Posted - 2012-02-29 : 13:37:56
Having trouble getting a result I want and can't find answer on internet.
When a row doesn't exist I want the value returned to = "NONE". Here is what I have, but can't seem to get working:

SELECT
CASE
WHEN [field] IS NULL THEN 'NONE'
ELSE [field]
END
FROM [table]

Problem with this is that it's truly not null as the row doesn't exist for some accounts. I fooled around with NOT EXIST statement, but was hoping for something simple where I didn't have to create a temp table. I'm also using a IN statment for WHERE clause with a list of 100 accounts, so was hoping to void doing subquery.

Anyone know of any shortcuts that I'm missing? Thanks!

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2012-02-29 : 13:42:03
A temp table is not needed to use NOT EXISTS.

IF NOT EXISTS (YourQueryGoesHere with * instead of your column list)
SELECT 'NONE'
ELSE
YourQueryGoesHere

Now if that's not what you want, then you need to be more explicit. You could also use @@ROWCOUNT instead of the above.

YourQueryGoesHere

IF @@ROWCOUNT = 0
SELECT 'NONE'

But better yet, just do this in your application. Your application will know that 0 rows were returned, so use your NONE thing in the application instead.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page
   

- Advertisement -