October 10, 2011 at 12:21 pm
I have a simple query:
SELECT name FROM atable WHERE a = 1
I want it to return Null if it finds nothing, but it just returns an empty set.
Is it possible to do this?
October 10, 2011 at 12:36 pm
Well by definition you can't return anything if there are no records. π You would have to force the query to always return a resultset.
Something like this:
select top 1 name from
(
SELECT name, 1 as SortOrder FROM atable WHERE a = 1
union select null, 2
) myTable
order by SortOrder
So you can force the issue but it seems this type of thing is more suited to the front end instead of trying to make sql return data when there is no data to return. This is forcing the square peg into the round hole.
_______________________________________________________________
Need help? Help us help you.
Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.
Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.
Cross Tabs and Pivots, Part 1 β Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/
October 11, 2011 at 4:40 am
tnocella (10/10/2011)
I have a simple query:SELECT name FROM atable WHERE a = 1
I want it to return Null if it finds nothing, but it just returns an empty set.
Is it possible to do this?
Maybe this:
if exists(SELECT name FROM atable WHERE a = 1)
SELECT name FROM atable WHERE a = 1
else
select null -- or 'Not found'
October 12, 2011 at 5:01 am
When you know there's only zero or one result row:
SELECT MAX(name) FROM atable WHERE a = 1
But maybe that's too simple ...
October 12, 2011 at 6:55 am
Never miss a (nearly) free opportunity to gather some information:
SELECT Result = NULLIF(COUNT(*),0) FROM atable
For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
October 12, 2011 at 7:08 am
I use the ISNULL() function for things of this nature.
SET @someval = ISNULL((SELECT Name FROM SOMETABLE WHERE ID=3),'')
OR
SELECT @someval = ISNULL(Name,'') FROM SOMETABLE WHERE ID=3
if the result is null, will replace it with '' (blank string).
October 12, 2011 at 7:20 am
SET @someval = ISNULL((SELECT Name FROM SOMETABLE WHERE ID=3),'')
OR
SELECT @someval = ISNULL(Name,'') FROM SOMETABLE WHERE ID=3
This assumes that the subquery returns no more than one row, which might not be valid.
The absence of evidence is not evidence of absence
- Martin Rees
The absence of consumable DDL, sample data and desired results is, however, evidence of the absence of my response
- Phil Parkin
October 12, 2011 at 8:03 am
True, assuming that ID = 3 can only occur on one record.
Viewing 8 posts - 1 through 7 (of 7 total)
You must be logged in to reply to this topic. Login to reply