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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T08:44:08+00:00 2026-05-21T08:44:08+00:00

I am designing a SQL query to extract all records from a given table.

  • 0

I am designing a SQL query to extract all records from a given table. But the trick here is that this logic is based on a numeric database field. So there are 4 choices: 0,1,2,3. If user selects 0,1, or 2, then my query returns rows with the specified value. But if they choose 3, it should return all of the rows. How do I do this in SQL? I know if this was a string, I could do something like:
WHERE = CASE WHEN = 3 THEN ‘%’ ELSE END

But in this case, is an integer. Sounds relatively simple but I’m getting errors.

  • 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-21T08:44:09+00:00Added an answer on May 21, 2026 at 8:44 am

    The simplest way is to do this:

    select MyColumn
    from   MyTable
    where  ( MyValue = @MyParameter or @MyParameter = 3)
    

    If your interested in better optimization, then you can do this, but it is less maintainable:

    if (@MyParameter = 3)
        select MyColumn
        from MyTable
    else
        select MyColumn
        from MyTable
        where MyValue = @MyParameter
    

    If I were forced to implement this functionality, then I would probably do this, just to make things clear:

    declare @AllRecords nchar(1)
    if (@MyParameter = 3)
       set @AllRecords = N'Y'
    else
       set @AllRecords = N'N'
    
    select MyColumn
    from MyTable
    where (MyValue = @MyParameter or @AllRecords = N'Y')
    

    Hopefully, I won’t ever have to implement a system that mixes flags and data value in this way.

    UPDATED

    Here is a version that should work with your expanded requirements (this requires one of the newer versions of SQL Server, I think):

    declare @SelectedLevels table (LevelId int not null primary key)
    if @LevelId = 3
        insert into @SelectedLevels (LevelId) values (1), (2)
    else if @LevelId = 5
        insert into @SelectedLevels (LevelId) values (0), (1), (2)
    else
        insert into @SelectedLevels (LevelId) values (@LevelId)
    
    select mt.MyColumn
    from   MyTable mt
           inner join @SelectedLevels sl on sl.LevelId = MyTable.LevelId
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am designing a query that pull rows from a large table based on
While designing a table my colleague here says that I should avoid identity column
When designing user table what would be the must have fields from the security/user
When designing an SQL query, which is faster, selecting the whole row (select *)
I'm designing a SQL datbase table and a couple of the columns need to
I'm designing a small SQL database to be used by a web application. Let's
When designing LINQ classes using the LINQ to SQL designer I've sometimes needed to
I'm designing my database and LINQ To SQL ASP.NET web application. Imagine I have
Designing a new system from scratch. I'll be using the STL to store lists
A colleague of mine is currently designing SQL queries like the one below to

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.