Gmail Calendar Documents Reader Web more »
Recently Visited Groups | Help | Sign in
Google Groups Home
SQL Case Select
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  6 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
MS Forum Newsgroup User  
View profile  
 More options Nov 21 2009, 11:52 am
Newsgroups: microsoft.public.sqlserver.programming
From: "MS Forum Newsgroup User" <u...@microsoft.com>
Date: Sat, 21 Nov 2009 16:52:35 -0000
Local: Sat, Nov 21 2009 11:52 am
Subject: SQL Case Select
Hope you can help, I thought this would work but doesn't,

SELECT getdata= case(@who)
                        When 'me' then (select col1, col2, col3)
                        When 'him' then (select col4, col5, col6)
            When 'them' then (select col1, col3, col5)
else '' end

From dbo.mytbl Where ID= @ID

End

Thanks for any help!


    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Plamen Ratchev  
View profile  
 More options Nov 21 2009, 1:04 pm
Newsgroups: microsoft.public.sqlserver.programming
From: Plamen Ratchev <Pla...@SQLStudio.com>
Date: Sat, 21 Nov 2009 13:04:49 -0500
Local: Sat, Nov 21 2009 1:04 pm
Subject: Re: SQL Case Select
CASE expressions return a scalar value with singe data type. If your columns are compatible data type then you can write
the query as follows:

SELECT CASE @who WHEN 'me' THEN col1
                  WHEN 'him' THEN col4
                  WHEN 'them' THEN col1
                  ELSE ''
        END AS col1,
        CASE @who WHEN 'me' THEN col2
                  WHEN 'him' THEN col5
                  WHEN 'them' THEN col3
                  ELSE ''
        END AS col2,
        CASE @who WHEN 'me' THEN col3
                  WHEN 'him' THEN col6
                  WHEN 'them' THEN col5
                  ELSE ''
        END AS col3
FROM dbo.mytbl
WHERE ID = @ID;

--
Plamen Ratchev
http://www.SQLStudio.com


    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
--CELKO--  
View profile  
 More options Nov 21 2009, 4:27 pm
Newsgroups: microsoft.public.sqlserver.programming
From: --CELKO-- <jcelko...@earthlink.net>
Date: Sat, 21 Nov 2009 13:27:35 -0800 (PST)
Local: Sat, Nov 21 2009 4:27 pm
Subject: Re: SQL Case Select
Let us start with basic programming and then get into SQL.  CASE is an
expression.  An expression has one and only one data type and returns
one and only one scalar value.

Now for the SQL:
1) SELECT always has a FROM; SELECT by itself is dialect
2) Please be consistent with capitalization so people can read your
code
3) RDBMS has no magical, universal "id" because it follows the logical
Law of Identity -- to be is to be something in particular; to be
nothing in particular or everything in general is to be nothing at
all.
4) Do not use the old proprietary "<column name> = <expression>"
syntax.  Please learn that it does not create rows, as you tried to
do.

Here is a stinky kludge:

SELECT CASE
       WHEN @who IN ('me', 'them')
       THEN col1
       WHEN @who = 'him'
       THEN col4
       ELSE '' END AS magic_something_1,

       CASE @who
       WHEN 'me' THEN col2
       WHEN 'them' THEN col3
       WHEN 'him' THEN col5
       THEN  ELSE '' END AS magic_something_2,

       CASE @who
       WHEN 'me' THEN col3
       WHEN 'them' THEN col5
       WHEN 'him' THEN co6
       THEN  ELSE '' END AS magic_something_3

 FROM Foobar
WHERE vague_magic_id = @in_vague_magic_id

Remember that freshman course on Software Engineering?  The
fundamentals of this craft? Flag coupling -- the worst way to write
code in any language?  "Britney Spears, Squids and Automobiles" is a
phrase that refers to a piece of crappy code that has no cohesion --
you have no idea what it does until run time.


    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
MS Forum Newsgroup User  
View profile  
 More options Nov 21 2009, 4:15 pm
Newsgroups: microsoft.public.sqlserver.programming
From: "MS Forum Newsgroup User" <u...@microsoft.com>
Date: Sat, 21 Nov 2009 21:15:51 -0000
Local: Sat, Nov 21 2009 4:15 pm
Subject: Re: SQL Case Select
Thanks Plamen,

Does this mean you are not able to use a separate SQL for a case?

Regards,

"Plamen Ratchev" <Pla...@SQLStudio.com> wrote in message

news:iqCdnRkWzeAntpXWnZ2dnUVZ_tJi4p2d@speakeasy.net...


    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Plamen Ratchev  
View profile  
 More options Nov 21 2009, 5:12 pm
Newsgroups: microsoft.public.sqlserver.programming
From: Plamen Ratchev <Pla...@SQLStudio.com>
Date: Sat, 21 Nov 2009 17:12:20 -0500
Local: Sat, Nov 21 2009 5:12 pm
Subject: Re: SQL Case Select
CASE is not a control of flow element and it can only return a scalar value. If you need to execute conditional SQL then
you can use IF...ELSE

IF @who = 'me'
SELECT ...
ELSE IF @who = 'him'
      SELECT ...
ELSE IF @who = 'them'
      SELECT ...

--
Plamen Ratchev
http://www.SQLStudio.com


    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
MS Forum Newsgroup User  
View profile  
 More options Nov 21 2009, 5:59 pm
Newsgroups: microsoft.public.sqlserver.programming
From: "MS Forum Newsgroup User" <u...@microsoft.com>
Date: Sat, 21 Nov 2009 22:59:36 -0000
Local: Sat, Nov 21 2009 5:59 pm
Subject: Re: SQL Case Select
Plamen,

That's what I was looking for thanks again, I was afraid that the If is not
available in SQL.

Thanks!

"Plamen Ratchev" <Pla...@SQLStudio.com> wrote in message

news:iqCdnRgWzeAg-JXWnZ2dnUVZ_tJi4p2d@speakeasy.net...


    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »

Create a group - Google Groups - Google Home - Terms of Service - Privacy Policy
©2010 Google