moot67:(
- Are there other uses for Coalesce vs replacement for ISNULL
This is my first post so pardon the formatting (or lack thereof).
ISNULL evaluates one item and does a substitution of like type if the value is null. Coalesce allows multiple items to be compared in one statement.
Here is a use that might be hard to do with ISNULL, where you are combining data from several tables:
Situation is where you need to combine information based on matches of one column of data in multiple tables.
Table1:
memberid initialjob 123 GS 124 PDR
Table2: memberid intermediatejob 123 AAP 125 AV
Table3: memberid lastjob 123 TS 126 CS
and you need to have all these tables into TABLEFINAL: memberid initialjob intermediatejob lastjob 123 GS AAP TS 124 PDR NULL NULL 125 NULL AV NULL 126 NULL NULL CS
|
For this you can use Coaelesce to combine the data even if there is no matching row in one or more of the tables:
DECLARE @t1 TABLE (memberid INT,initialjob VARCHAR(50))
DECLARE @t2 TABLE (memberid INT,intermediatejob VARCHAR(50))
DECLARE @t3 TABLE (memberid INT,lastjob VARCHAR(50))
INSERT INTO @t1
SELECT 123,'GS' UNION
SELECT 124,'PDR'
INSERT INTO @t2
SELECT 123,'AAP' UNION
SELECT 125,'AV'
INSERT INTO @t3
SELECT 123,'TS' UNION
SELECT 126,'CS'
set statistics io on
set statistics time on
select coalesce(t1.memberid,t2.memberid,t3.memberid) 'memberid', initialjob, intermediatejob, lastjob
from @t1 t1
full outer join @t2 t2 on t1.memberid=t2.memberid
full outer join @t3 t3 on t3.memberid=t2.memberid
--order by 1 (If you so desire)
Toni...