Reputation: 1911
Below is my SQL Table,
how do i get the result from this table as pack_cd and date which is not null. e.g.
from SQL Table i want to get PACK_CD
and date as
ETDT
if its NOT NULL
PPDT
if ETDT IS NULL
and PPDT
is NOT NULL
PDT
if ETDT IS NULL
and PPDT
is NULL
and PDT
is NOT
NULL
Upvotes: 0
Views: 218
Reputation: 5798
This is alternate option to understand. The same you achieve COALESCE as @wewesthemenace suggested.
SELECT
PACK_CD,
DATE = case when isnull(ETDT,'') != '' then ETDT
else
case when isnull(PPDT,'') != '' then PPDT
else
PDT
end
end
FROM [YourTable]
Upvotes: 0
Reputation: 31879
Use COALESCE
:
SELECT
PACK_CD,
DATE = COALESCE(ETDT, PPDT, PDT)
FROM [YourTable]
If you want to show only those rows with NULL
column, just add a WHERE
clause:
WHERE
ETDT IS NULL
OR PPDT IS NULL
OR PDT IS NULL
Upvotes: 4