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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T20:59:51+00:00 2026-06-15T20:59:51+00:00

Below is my where clause of the query when i want data for a

  • 0

Below is my where clause of the query when i want data for a specific user

where Completion_Date>= '11/01/2011' 
and Completion_Date<= '12/11/2012' 
and System_user_id = 1234

and below is the where clause when i want to pull data for all the user:

where Completion_Date>= '11/01/2011' 
and Completion_Date<= '12/11/2012'

Since i dont want 2 seperate queries, is there a way to add a condition in where clause so that i can use a single query and depending on the input (i.e System_user_id) it will decide on whether to add extra condtion in the query.
I will be sending -1 when i want data for all users & for a specific user its system_user_id will be sent.

  • 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-15T20:59:52+00:00Added an answer on June 15, 2026 at 8:59 pm

    You may try the following procedure.

    Updated Query

    declare @userid int = -1
    if (@userid = -1)
        BEGIN
    
         SELECT * FROM mytable 
         where Completion_Date>= '11/01/2011' 
         and Completion_Date<= '12/11/2012' 
         and userid in 
             (select distinct userID from mytable)
        end
    ELSE
        BEGIN
    
         SELECT * FROM mytable 
         where Completion_Date>= '11/01/2011' 
         and Completion_Date<= '12/11/2012' 
         and userid = @userid 
    
    end;
    

    Results:

    USERID  NAME    COMPLETION_DATE
    123     john    2011-11-01
    125     tim     2011-11-02
    127     ron     2011-11-08
    
    • REference SQLFIDDLE

    To see a specific user:

    • Reference SQLFIDDLE

    ANOTHER METHOD

    Updated after latest comment from OP

    • SQLFIDDLE

    Query:

    DECLARE @uid int = -1
    SELECT 
    *
    FROM mytable 
    WHERE 
    ( CASE 
         WHEN @uid <> -1 THEN @uid
         ELSE userid
      END
    ) = userid
    
    and Completion_Date>= '11/01/2011' 
         and Completion_Date<= '12/11/2012' 
    ;
    

    Results: when @uid = -1

    USERID  NAME    COMPLETION_DATE
    123     john    2011-11-01
    125     tim     2011-11-02
    127     ron     2011-11-08
    

    Please comment if you have tried this out 🙂

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

Sidebar

Related Questions

hopefully you can get what I want to do from the mysql query below.
I have a number of database tables below. I want to display each user
I want to fire below query on tm_matrix but gives me following error mysql>
I am getting missing FROM clause error (shown below) I want to find the
In my example query below, I am pulling all text that matches a keyword
The code below works, but all of the data displays in one row(but different
How can you put an array to the WHERE -clause below without creating 50
I have a method to Generate Expression By Clause as below: internal static Expression<Func<TModel,
When I have a SQLConnection within a using clause as illustrated below do I
I need help with the SQL ORDER BY clause. In my table below I

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.