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

  • Home
  • SEARCH
  • 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 8648421
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T13:18:34+00:00 2026-06-12T13:18:34+00:00

I am trying to do a select statment in tsql with a case within

  • 0

I am trying to do a select statment in tsql with a case within a case. First is based on what the SearchField is. Next I need to do it based on the SearchOper.

    declare @searchField varchar(50)  
    declare @searchString varchar(50)
    declare @searchOper varchar(50)

    case @searchField
     when 'CompanyName' then
        case @searchOper
          when 'eq' then
            select * from tbl1 where CompanyName = @searchString
         when 'ne' then 
           select * from tbl1 where CompanyName <> @searchString
         end
     when 'StoreNum' then
        case @searchOper
          when 'eq' then
             select * from tbl1 where StoreNum = @searchString
          when  'ne' then
             select * from tbl1 where StoreNum <> @searchString
        end 
     end 

Note what I am trying to do is within the case statement do a select.

I get a message saying Incorrect syntax near the keyword ‘case’.

  • 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-12T13:18:35+00:00Added an answer on June 12, 2026 at 1:18 pm

    Based on your example, you probably want to use IF statements (here is an example with ELSE, but you can use IF and ELSE IF multiple times too):

    declare @searchField varchar(50)  
    declare @searchString varchar(50)
    declare @searchOper varchar(50)
    
    IF @searchField='CompanyName'
    BEGIN
        IF @searchOper='eq'
        BEGIN
            select * from tbl1 where CompanyName = @searchString
        END
        ELSE
        BEGIN
            select * from tbl1 where CompanyName <> @searchString
        END
    END
    ELSE 
    BEGIN
        IF @searchOper='eq'
        BEGIN
            select * from tbl1 where StoreNum = @searchString
        END
        ELSE
            select * from tbl1 where StoreNum <> @searchString
        END
    END
    

    CASE is very similar, but is rather used with inline statements (i.e. SELECT CASE 1=1 THEN ‘a’ ELSE ‘b’ END).

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

Sidebar

Related Questions

I am trying to have a running average column in the SELECT statement based
I have very long select query which i need to filter based on some
I am trying to concatenate the select statment results in one row. For eg
Is there a way in TSQL to do something like this: select a,b,c, case
I'm trying to select only the User_IDs that are in the first select statement
Consider the following TSQL: declare @xml xml select @xml = '<test xmlns=http://this-is-the-default-namespace-uri>some data</test>' select
I am trying to execute a prepared statment using the following: dbaBean.setPrepStmt(dbaBean.getConn().prepareStatement( SELECT id,
Code is running fine when commenting DELETE statement and trying with SELECT statement. Please
I'm trying to select records with a statement SELECT * FROM A WHERE LEFT(B,
I'm trying to execute a SELECT statement that includes a column of a static

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.