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 8090009
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T19:35:00+00:00 2026-06-05T19:35:00+00:00

Basically, I’m wondering if it’s possible to take this IF @Value = 1 SELECT

  • 0

Basically, I’m wondering if it’s possible to take this

IF @Value = 1
    SELECT col1, col2 FROM MyTable
ELSE
    SELECT col2 FROM MyTable

And do it in a single statement? Something like

SELECT IF(@Value = 1 col1), col2 FROM MyTable

I’ve tried this

SELECT case @Value when 1 then col1 end, col2 FROM MyTable

It always returns 2 columns, but the first will be null when @Value is not 1. I’d like it to return 2 columns only if @Value is 1, and one column the rest of the time.

  • 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-06-05T19:35:02+00:00Added an answer on June 5, 2026 at 7:35 pm

    With dynamic SQL, yes.

    DECLARE @sql NVARCHAR(MAX);
    SELECT @sql = N'SELECT ' + CASE WHEN @Value = 1 THEN
      'col1, ' ELSE '' END + 'col2 FROM dbo.MyTable;';
    PRINT @sql;
    --EXEC sp_executesql @sql;
    

    Without dynamic SQL you can’t really change the shape of a resultset.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Basically from a database I am getting data that is formatted like this nameofproject101
Basically this function is meant to store the height value of the element that
Basically I want to achieve this workflow: Checkout from repository on windows system (or
Basically, what signal does '0' represent, because here I see SIGNAL numbers starting from
Basically i'd like to allow an arbitrary (but not empty ) number of key-value
Basically I need to be able to do this: var obj = {foo:bar}, arr
Basically I'm attempting to add rows to a table, I need to do this
Basically I have an iframe loaded that is accessed from the parent whenever it
Basically I have a loop incrementing i, and I want to do this: var
Basically I have a single element inside of an xml file where I store

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.