I have two columns with data from Table1 that have the SKU number and New SKU Number. My goal is to query this data as a single column as shown in the 'Desired' Table
------- -------- ---------
| SKU | NEW_SKU| | Desired |
------- -------- |---------
| null | 1 | | 1 |
| null | 1 | | 1 |
| 2 | null | | 2 |
| 2 | 3 | | 3 |
| 2 | 5 | | 5 |
------- -------- ---------
Currently I have:
SELECT
SKU
CASE WHEN (SKU IS NOT NULL AND NEW_SKU IS NOT NULL) THEN new_sku
WHEN (SKU IS NULL AND NEW_SKU IS NOT NULL) THEN new_sku
ELSE SKU END AS Product_number
FROM Table1
CodePudding user response:
Use COALESCE
here:
SELECT SKU, NEW_SKU, COALESCE(NEW_SKU, SKU) AS Product_number
FROM Table1;
The COALESCE
function, as used above, would use the NEW_SKU
value if it be non NULL
, otherwise would fall back to using the SKU
value.