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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T21:29:38+00:00 2026-05-16T21:29:38+00:00

I need to include a column name in the where clause of SQL query

  • 0

I need to include a column name in the where clause of SQL query depending on a variable value.
For Example,
1. If I have a variable,say,@test and I assigned the variable with the value 1/0.
2. I have a table ,say, table1 with 2 column names col1,col2.

Now, I am writing a query to fetch some data from the table by including only one column at a time depends on the @test variable(declared earlier) i.e. I need to include col1 in the where clause of the query if the @test is 1 and col2 if @test is 0.

Please let me know the solution asap.

Thanks in advance

  • 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-16T21:29:39+00:00Added an answer on May 16, 2026 at 9:29 pm
    declare @tbl table
    (
        col1 int,
        col2 int
    
    )
    
    insert into @tbl select 10, 20
    
    declare @test int
    
    set @test = 1
    
    select *
    from @tbl
    Where Case When @test = 1 Then Col1 Else Col2 End  = 10
    

    If datatype is different

    select *
    from @tbl
    Where Case when @test = 1 Then Col1 end = 10
            OR
          Case when @test = 2 Then Col2 end = 'sometext'
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Say I have a bunch of data on some people. This could include Name,
I have text file(test.data), which include(this is just example other files have more values....)
I have a very simple MS SQL table with the following data(with column name
I have a .txt file that I need to include into a page, that
I have a prototype model where I need to include the following extension methods
i need to specify following in one query: It should include the Activity_name and
i need to specify following in one query: It should include the Activity_name and
I'm trying to construct a query that will include a column indicating whether or
I have a relatively simple pivot/transform query, which appears as follows: TRANSFORM Sum(T1.VALUE) As
I need to include a copyright statement at the top of every Python source

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.