April 28, 2015 at 8:55 pm
Comments posted to this topic are about the item COALESCE in Left Join
Thanks,
Naveen.
Every thought is a cause and every condition an effect
April 29, 2015 at 12:55 am
Good brain training!
Thanks!
😀
April 29, 2015 at 1:07 am
Only 54% correct? I was expecting close to 100%!
April 29, 2015 at 1:26 am
Great question, thanks.
Need an answer? No, you need a question
My blog at https://sqlkover.com.
MCSE Business Intelligence - Microsoft Data Platform MVP
April 29, 2015 at 1:27 am
Toreador (4/29/2015)
Only 54% correct? I was expecting close to 100%!
It depends if one has enough time to read correctly the question of th day.
April 29, 2015 at 2:23 am
The question assumes SET ANSI_NULLS ON
Although it is ON by default, and will stay ON on later SQL version it's not always the case on older SQL Server versions.
April 29, 2015 at 4:38 am
Well I tried it and got 7 and 7.
create table #abc (a int, b int, c int);
INSERT INTO #abc values
(NULL,NULL,3),
(1,NULL,3),
(1,NULL,NULL),
(NULL,2,NULL);
Create table #xyz (x int, y int, z int);
INSERT INTO #abc values
(NULL,2,3),
(NULL,NULL,1),
(NULL,NULL,2);
SELECT
*
FROM
#abc a
LEFT JOIN #xyz x
ON a.a = x.x
AND a.b = x.y;
-- Q2
SELECT
*
FROM
#abc a
LEFT JOIN #xyz x
ON COALESCE(a.a, 9) = COALESCE(x.x, 9)
AND COALESCE(a.b, 9) = COALESCE(x.y, 9);
drop table #abc;
drop table #xyz;
April 29, 2015 at 5:01 am
Probably because you've got a typo:
Create table #xyz (x int, y int, z int);
INSERT INTO #abc values
(NULL,2,3),
(NULL,NULL,1),
(NULL,NULL,2);
April 29, 2015 at 5:11 am
Thanks! Copy paste will get you in trouble.:-D
April 29, 2015 at 5:16 am
Good question to start the day. An exercise in clear thinking is always good for the brain. Thanks.
April 29, 2015 at 6:40 am
Thanks. Good training on COALESCE which I rarely use.
April 29, 2015 at 7:50 am
How about use ISNULL?
-- Q3
SELECT
*
FROM
#abc a
LEFT JOIN #xyz x
ON ISNULL(a.a, 9) = ISNULL(x.x, 9)
AND ISNULL(a.b, 9) = ISNULL(x.y, 9);
--------------------------------------
;-)“Everything has beauty, but not everyone sees it.” ― Confucius
April 29, 2015 at 8:59 am
chgn01 (4/29/2015)
How about use ISNULL?-- Q3
SELECT
*
FROM
#abc a
LEFT JOIN #xyz x
ON ISNULL(a.a, 9) = ISNULL(x.x, 9)
AND ISNULL(a.b, 9) = ISNULL(x.y, 9);
In this case, they'll act the same way. But that won't be always the case.
April 29, 2015 at 9:26 am
Thanks for this interesting question!
April 29, 2015 at 9:39 am
Luis Cazares (4/29/2015)
chgn01 (4/29/2015)
How about use ISNULL?-- Q3
SELECT
*
FROM
#abc a
LEFT JOIN #xyz x
ON ISNULL(a.a, 9) = ISNULL(x.x, 9)
AND ISNULL(a.b, 9) = ISNULL(x.y, 9);
In this case, they'll act the same way. But that won't be always the case.
If there's no coercing data going on, it should be the same. There's probably some esoteric exception though.
Don Simpson
Viewing 15 posts - 1 through 15 (of 19 total)
You must be logged in to reply to this topic. Login to reply