November 17, 2004 at 12:24 pm
I got the following data in a table
Col1 Col2
HHHH TTTTT
HHHH RRRRR
HHHH SSSSS
AAAA FFFFF
BBBB GGGGG
CCCC HHHHH
DDDD IIIII
I want the follwing result set
TTTTT
RRRRR
SSSSS
AAAA
BBBB
CCCC
DDDD
Means where ever i see HHHH in Col1 i have to select Col2, Can you help on this.
Thanks in Advance
November 17, 2004 at 12:40 pm
How bout
CASE Col1 = 'HHHH' THEN Col2 ELSE Col1 END AS SReturn ???
You may want to review COALESCE as well..
Good Hunting!
AJ Ahrens
webmaster@kritter.net
November 17, 2004 at 12:41 pm
Thanks!, It worked
November 17, 2004 at 12:48 pm
How would this look like with COALESCE?
--
Frank Kalis
Microsoft SQL Server MVP
Webmaster: http://www.insidesql.org/blogs
My blog: http://www.insidesql.org/blogs/frankkalis/[/url]
November 17, 2004 at 5:12 pm
I honestly don't know Frank. I know that I generally forget about COALESCE until I read about it here. Figured the CASE and COALESCE in certain instances go together. ![]()
Good Hunting!
AJ Ahrens
webmaster@kritter.net
November 18, 2004 at 1:06 am
Hey, I wouldn't use COALESCE unless I really need the extended functionality compared to ISNULL or have to care about portability. Why? Have a play with this snippets.
CREATE TABLE #t
(
c1 CHAR
)
INSERT INTO #t VALUES (NULL)
SELECT
ISNULL(c1,'Frank')
, COALESCE(c1,'Frank')
FROM
#t
SELECT ISNULL(c1,'Frank')
, COALESCE(c1,'Frank')
,CASE WHEN c1 IS NULL THEN 'Frank' ELSE c1 END
FROM
#t
DROP TABLE #t
SELECT
7 / ISNULL(CAST(NULL AS int), 2.00)
, 7 / COALESCE(CAST(NULL AS int), 2.00)
, 7 / CASE WHEN CAST(NULL AS int) IS NULL THEN 2.00 END
Now, run this and then read the explanations for both commands WORD by WORD in BOL ![]()
The following is by Umachandar Jayachandran from the MS newsgroups. Compare the execution plans ![]()
select
coalesce((
select
a2.au_id
from
pubs..authors a2
where
a2.au_id = a1.au_id ),'')
from
pubs..authors a1
select
isnull((
select
a2.au_id
from
pubs..authors a2
where
a2.au_id = a1.au_id ),'')
from
pubs..authors a1
--
Frank Kalis
Microsoft SQL Server MVP
Webmaster: http://www.insidesql.org/blogs
My blog: http://www.insidesql.org/blogs/frankkalis/[/url]
November 18, 2004 at 1:08 am
...and I forgot, I haven't verfiy this myself, but I was told that the difference in execution plans is getting bigger with SQL Server 2005.
![]()
--
Frank Kalis
Microsoft SQL Server MVP
Webmaster: http://www.insidesql.org/blogs
My blog: http://www.insidesql.org/blogs/frankkalis/[/url]
November 18, 2004 at 5:59 am
Probably another VERY GOOD reason that I forget about COALESCE ![]()
Thank you as always for keeping me from hurting myself
Good Hunting!
AJ Ahrens
webmaster@kritter.net
November 18, 2004 at 6:54 am
AJ,
If your interested this would be the solution using COALESCE
COALESCE(NULLIF(Col1,'HHHH'), Col2) AS [Result]
Far away is close at hand in the images of elsewhere.
Anon.
November 18, 2004 at 6:56 am
Hey, how do you say: Mr. Knows-it-all ? scnr
![]()
--
Frank Kalis
Microsoft SQL Server MVP
Webmaster: http://www.insidesql.org/blogs
My blog: http://www.insidesql.org/blogs/frankkalis/[/url]
November 18, 2004 at 8:11 am
I just coalesce when I'm in meetings with clients. It makes me sound important and dba-ish.
Quand on parle du loup, on en voit la queue
November 18, 2004 at 8:18 am
Okay, that's acceptable.
I guess you would be lost without an instrument that could deal with missing information in that clientel anyway. ![]()
--
Frank Kalis
Microsoft SQL Server MVP
Webmaster: http://www.insidesql.org/blogs
My blog: http://www.insidesql.org/blogs/frankkalis/[/url]
Viewing 12 posts - 1 through 12 (of 12 total)
You must be logged in to reply to this topic. Login to reply