Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Return a value if no record is found

I have this simple statement that works:

SELECT idnumber FROM dbo.database WHERE number = '9823474' 

If the number does not exist anywhere in the table, it fails. I would like to add something to this statement that says:
IF NO RECORD IS FOUND RETURN NULL INSTEAD OF NO ROW.

Any suggestions?

like image 868
user1042304 Avatar asked Nov 11 '11 19:11

user1042304


People also ask

How do I return NULL if no records found SQL?

There are two ways to replace NULL with blank values in SQL Server, function ISNULL(), and COALESCE(). Both functions replace the value you provide when the argument is NULL like ISNULL(column, ”) will return empty String if the column value is NULL.

How do you handle no data found in SQL query?

no_data_found :- Whenever PL/SQL Block having select into clause and also if requested data is not available then oracle server returns an error ora – 1403 : no data found. For handling this error oracle provided no_data_found exception name. Your employee doesn't exists.

How do I make a count 0 return in SQL?

An ugly workaround, if you want your original query to return a row with 0's, when no records are present, is to add something like this to your query: UNION SELECT NULL AS [Month], 0 AS [COUNT], 0 AS [GRAMS], 0 AS [PRINCIPAL] WHERE (SELECT COUNT(*) FROM #AllExpired) = 0 , but a better solution would be to have your ...

What does return 0 do in SQL Server?

In default, when we execute a stored procedure in SQL Server, it returns an integer value and this value indicates the execution status of the stored procedure. The 0 value indicates, the procedure is completed successfully and the non-zero values indicate an error.


1 Answers

Encapsulate the query in a sub-query to transform "no row" to a NULL value.

I tested and verified this with PostgreSQL, SQL Server and MySQL. Also works with SQLite.

SELECT (SELECT id FROM tbl WHERE id = 9823474) AS id; 

In Oracle you have to select from the dummy 1-row table DUAL like this:

SELECT (SELECT id FROM tbl WHERE id = 9823474) AS id FROM DUAL; 

You can do the same in MySQL for compatibility reasons, but you don't have to.
Similar in Firebird:

SELECT (SELECT id FROM tbl WHERE id = 9823474) AS id FROM RDB$DATABASE; 

This does it for DB2 (like Sean commented):

SELECT (SELECT id FROM tbl WHERE id = 9823474) AS id FROM SYSIBM.SYSDUMMY1; 

Alternative with UNION ALL

SELECT id FROM tbl WHERE id = 9823474 UNION  ALL SELECT NULL -- FROM DUAL  -- for Oracle FETCH FIRST 1 ROW ONLY; 

Standard SQL, but I only tested this with Postgres, which evaluates like this:
If a row is found in the first SELECT, it is returned. Postgres stops looking for more rows, as soon as the first is found due to LIMIT 1 (FETCH FIRST 1 ROW ONLY).
The second SELECT is only even executed if the first returns nothing. The data type of the NULL value is determined by the data type of tbl.id automatically.

About the LIMIT clause:

  • ANSI/ISO plans for LIMIT standardization?
like image 118
Erwin Brandstetter Avatar answered Sep 17 '22 17:09

Erwin Brandstetter