1

I need to know what SQL statement do I use to produce an output where if a cell contains null then it pulls the information from the column next to it.

To explain better: I have three tables, table 1 contains 7 columns, table 2 contains 6 columns, table 3 contains 4 columns.

I need select 1 column from table 1 followed by a dash then select 3 columns from another table followed by a dash and then another column from a different table,but where ever a column contains null, the values must be replaced by a dash '-'.

This is the code I have so far :

SELECT 
    Episode_Name, '-', Pnr_FName, Pnr_LName, Guest, '-', Car_Make
FROM 
    EPISODES, PRESENTERS, CARS

I did try:

(CASE WHEN Guest = null THEN ''
END EPISODE)

and I bound it in the select statement but it did not work.

Any help would be appreciated! thanks

1
  • Bad habits to kick : using old-style JOINs - that old-style comma-separated list of tables style was replaced with the proper ANSI JOIN syntax in the ANSI-92 SQL Standard (more than 20 years ago) and its use is discouraged Commented Aug 20, 2016 at 9:08

2 Answers 2

1

You can't check a column value for NULL using the = operator, because the result will also be NULL. Instead, you should use IS NULL and IS NOT NULL. But for readability purposes, I would use COALESCE instead of CASE statements.

Use COALESCE:

SELECT CONCAT(COALESCE(Episode_Name, ' '),
              '-',
              COALESCE(Pnr_FName, ' '),
              COALESCE(Pnr_LName, ' '),
              COALESCE(Guest, ' '),
              '-',
              COALESCE(Car_Make, ' '))
FROM EPISODES, PRESENTERS, CARS

By the way, you should use explicit join syntax instead of using commas in the FROM clause, e.g.

FROM EPISODES e INNER JOIN PRESENTERS p
    ON e.col = p.col
Sign up to request clarification or add additional context in comments.

Comments

0

I did try : (CASE WHEN Guest = null THEN '' END EPISODE)

try

case when guest is null then '' end 

You can't compare nulls,to check if nulls are present ,you have to use IS NULL..

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.