Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6887687
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T05:54:54+00:00 2026-05-27T05:54:54+00:00

I would like to return multiple values in my case statement, such as :

  • 0

I would like to return multiple values in my case statement, such as :

SELECT
  CASE
    WHEN <condition 1> THEN <value1=a1, value2=b1>
    WHEN <condition 2> THEN <value1=a2, value2=b2>
    ELSE <value1=a3, value3=b3>
  END
FROM <table>

Of course I can write the case condition multiple times, each time return one value. However, as I have many condition need to fit, say 100. It is not good to repeat case condition again and again.

Another question I would like to ask, what happend if one record fit multiple condition? does that mean it will return all of them or just the last one? e.g. condition 1 may become a subset of condition 2. what will happen?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T05:54:55+00:00Added an answer on May 27, 2026 at 5:54 am

    The basic way, unfortunately, is to repeat yourself.

    SELECT
      CASE WHEN <condition 1> THEN <a1> WHEN <condition 2> THEN <a2> ELSE <a3> END,
      CASE WHEN <condition 1> THEN <b1> WHEN <condition 2> THEN <b2> ELSE <b3> END
    FROM 
      <table> 
    

    Fortunately, most RDBMS are clever enough to NOT have to evaluate the conditions multiple times. It’s just redundant typing.

    In MS SQL Server (2005+) you could possible use CROSS APPLY as an alternative to this. Though I have no idea how performant it is…

    SELECT
      *
    FROM
      <table>
    CROSS APPLY
      (
       SELECT a1, b1 WHERE <condition 1>
       UNION ALL
       SELECT a2, b2 WHERE <condition 2>
       UNION ALL
       SELECT a3, b3 WHERE <condition 3>
      )
      AS case_proxy
    

    The noticable downside here is that there is no ELSE equivalent and as all the conditions could all return values, they need to be framed such that only one can ever be true at a time.

    EDIT

    If Yuck’s answer is changed to a UNION rather than JOIN approach, it becomes very similar to this. The main difference, however, being that this only scans the input data set once, rather than once per condition (100 times in your case).

    EDIT

    I’ve also noticed that you may mean that the values returned by the CASE statements are fixed. All records that match the same condition get the exact sames values in value1 and value2. This could be formed like this…

    WITH
      checked_data AS
    (
      SELECT
        CASE WHEN <condition1> THEN 1
             WHEN <condition2> THEN 2
             WHEN <condition3> THEN 3
             ...
             ELSE                   100
        END AS condition_id,
        *
      FROM
        <table>
    )
    ,
      results (condition_id, value1, value2) AS
    (
       SELECT 1, a1, b1
       UNION ALL
       SELECT 2, a2, b2
       UNION ALL
       SELECT 3, a3, b3
       UNION ALL
       ...
       SELECT 100, a100, b100
    )
    SELECT
      *
    FROM
      checked_data
    INNER JOIN
      results
        ON results.condition_id = checked_data.condition_id
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like my MSSQL statement to return the string 'multiple' if multiple jobs
I would like to return a string with all of the contents of a
I would like to return the id of a newly created object for my
I would like to return an array of string in my web services I've
I have a method where I would like to return an object instance of
I have a script with a factory method that I would like to return
Given the following XML, I would like to return all eventtitles where the eventtype
Given the following directory: string fullpath = C:\MyDir1\MyDir2\MyDir3; I would like to return MyDir3
I'm constructing a regex that is looking for dates. I would like to return
I am using matplotlib in a django app and would like to directly return

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.