Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL Replace NULL Values with Own text

Tags:

sql

postgresql

I need to display the keyboard players from a list of bands, and I've been able to using the following SQL:

SELECT BAND.NAME AS Band_Name, KBPLAYER.NAME AS Keyboard_Player
FROM BAND
FULL OUTER JOIN (
    SELECT M.NAME, MO.BID
    FROM MEMBEROF MO, MEMBER M
    WHERE MO.INSTRUMENT='keyboards'
    AND M.MID=MO.MID
    ) KBPLAYER
ON BAND.BID=KBPLAYER.BID
ORDER BY BAND.NAME, KBPLAYER.NAME

The above query displays the names of all the band and the keyboard player (if any) in that band, but I also want to display 'No KeyBoard Players' for those bands that don't have a keyboard player. How can I achieve this? Please let me know if you need me to furnish with details of the table structure.

Update: Please note that I'm not able to use any of the SQL3 procedures (COALESCE, CASE, IF..ELSE). It needs to conform strictly to SQL3 standard.

like image 309
Kuan E. Avatar asked Feb 05 '14 06:02

Kuan E.


People also ask

How do you replace NULL values in SQL with text?

Null Values can be replaced in SQL by using UPDATE, SET, and WHERE to search a column in a table for nulls and replace them. In the example above it replaces them with 0.

How do you replace NULL values in SQL with data?

We can replace NULL values with a specific value using the SQL Server ISNULL Function. The syntax for the SQL ISNULL function is as follow. The SQL Server ISNULL function returns the replacement value if the first parameter expression evaluates to NULL.

Which function is used to substitute NULL values in a query result?

In SQL Server, ISNULL() function is used to replace NULL values.

What is NVL and Coalesce in SQL?

NVL : Converts null value to an actual value. NVL2 : If first expression is not null, return second expression. If first expression is null, return third expression. the first expression can have any data type. COALESCE : Return first not null expression in the expression list.


1 Answers

Use the coalesce function. This function returns the first of it's arguments that are not null. Eg:

COALESCE(KBPLAYER.NAME,'No KeyBoard Players') AS Keyboard_Player
like image 68
harmic Avatar answered Oct 12 '22 08:10

harmic