0

I have two tables.

table 1:

-------------------------------
| product_id | product number |
-------------------------------
|    1001    |   67E432D      |
|    1002    |   888CDE32     | 
|    1003    |   54D32EC2     |
-------------------------------

table 2:

--------------------------
| product_id |   desc    |
--------------------------
|  1001      | product 1 |
|  1003      | peoduct 3 |
--------------------------

After joining table 1 and table 2 I get the following result.

-------------------------------------------
| product_id | product number |   desc    |
-------------------------------------------
|    1001    |   67E432D      | product 1 |
|    1002    |   888CDE32     |           |
|    1003    |   54D32EC2     | product 3 |
-------------------------------------------

Now as you can see that the 'desc' column of product 3 is empty now. How may I have a 0 in that column? Something like:

-------------------------------------------
| product_id | product number |   desc    |
-------------------------------------------
|    1001    |   67E432D      | product 1 |
|    1002    |   888CDE32     |     0     |
|    1003    |   54D32EC2     | product 3 |
-------------------------------------------

There might be some function which can do the job but I guess I am not aware of it..yet.

David Aldridge
  • 51,479
  • 8
  • 68
  • 96
Jaanna
  • 1,620
  • 9
  • 26
  • 46

2 Answers2

4

It sounds like you just want to use NVL

SELECT product_id, product_number, NVL( desc, '0' )
  FROM table_1
       LEFT OUTER JOIN table_2 USING (product_id)
ORDER BY product_id

SQLFiddle Demo

Himanshu
  • 31,810
  • 31
  • 111
  • 133
Justin Cave
  • 227,342
  • 24
  • 367
  • 384
1

As this question is tagged with Oracle, Justin's answer is fine.

If you care about portability, consider using coalesce: Based on Justin's answer:

SELECT product_id, product_number, coalesce( desc, '0' )
  FROM table_1
       LEFT OUTER JOIN table_2 USING (product_id)
ORDER BY product_id

As for the difference, look here

Community
  • 1
  • 1
Beryllium
  • 12,808
  • 10
  • 56
  • 86