Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

if condition in where clause of sql query

Tags:

sql-server

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.

like image 269
avinash chavan Avatar asked Dec 13 '12 10:12

avinash chavan


People also ask

Can I put an if statement in a WHERE clause SQL?

IF… ELSE clause is very handy and whenever you need to perform any conditional operation, you can achieve your results using it. But there are some limitations in IF… ELSE, and one of the limitations is that you cannot use it in WHERE clause.

Can we write if condition in SQL query?

We can use SQL IF statement without ELSE as well. In the following, the expression evaluates to TRUE; therefore, it prints the message. If the expression evaluates to FALSE, it does not return any output. We should use ELSE statement so that if an evaluation is not TRUE, we can set default output.

Which conditions can we use with WHERE clause?

The SQL WHERE clause is used to specify a condition while fetching the data from a single table or by joining with multiple tables. If the given condition is satisfied, then only it returns a specific value from the table. You should use the WHERE clause to filter the records and fetching only the necessary records.

How do you write if else condition in SQL query?

This is why you can nest IF ELSE in SQL query statements. It is demonstrated below: DECLARE @age INT; SET @age = 60; IF @age < 18 PRINT 'underage'; ELSE BEGIN IF @age < 50 PRINT 'You are below 50'; ELSE PRINT 'Senior'; END; In this example, the code will print underage if the value of @age is below 18.


2 Answers

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 :)

like image 137
bonCodigo Avatar answered Sep 21 '22 20:09

bonCodigo


Try:

WHERE ((@System_user_id = -1)
  AND (Completion_Date >= '11/01/2011') 
  AND (Completion_Date <= '12/11/2012'))
OR ((@System_user_id <> -1) 
  AND (System_user_id = @System_user_id) 
  AND (Completion_Date >= '11/01/2011') 
  AND (Completion_Date <= '12/11/2012'))

A variation on this using a Common Table Expression (SQL Fiddle)

;WITH CompletionDates AS
(
    SELECT *
    FROM MyTable
    WHERE Completion_Date >= '11/01/2011'
    AND Completion_Date <= '12/11/2012'
)
SELECT * 
FROM CompletionDates
WHERE (@System_user_id = -1) OR (System_user_id = @System_user_id)
like image 38
Ɖiamond ǤeezeƦ Avatar answered Sep 24 '22 20:09

Ɖiamond ǤeezeƦ