Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL Server CASE clause on where inside parenthesis

I have this problem in my SQL code:

I will only show my WHERE clause, because it is a bit long, this is it:

where 
 ((@account_status = 1027 AND a.AccountStatus = 1027  AND a.FolioNo = 
  @folio_no AND b.ReservationNo = @reservation_id)) OR
 ((@account_status = 1026 AND a.AccountStatus = 1026  AND a.FolioNo = 
 @folio_no AND b.ReservationNo = @reservation_id)) OR
 ((@account_status = 1025 AND a.AccountStatus = 1025 AND @trans_code = 1 AND 
 a.AccountStatementTransCode = 1 AND b.FolioNo = @folio_no AND 
 b.ReservationNo = @reservation_id)) OR
 ((@account_status = 1025 AND a.AccountStatus = 1025 AND @trans_code != 1 
 AND a.AccountStatementTransCode != 1 AND b.FolioNo = @folio_no AND 
 b.ReservationNo = @reservation_id)) OR
 ((a.FolioNo = @folio_no AND b.ReservationNo = @reservation_id AND 
 @trans_code = 2 AND a.AccountStatementTransCode = 2 AND 
 case
   when @sub_category = 14 then i.category_id is null
    else i.category_id = @sub_category 
 end )) OR
 ((a.FolioNo = @folio_no AND b.ReservationNo = @reservation_id AND 
 @trans_code = 3 AND a.AccountStatementTransCode = 3 AND i.category_id = 
 @sub_category)) OR
 ((a.FolioNo = @folio_no AND b.ReservationNo = @reservation_id AND 
 @trans_code = 4 AND a.AccountStatementTransCode = 4 AND i.category_id = 
 @sub_category)) OR
 ((a.FolioNo = @folio_no AND b.ReservationNo = @reservation_id AND 
 @trans_code = 5 AND a.AccountStatementTransCode = 5 AND i.category_id = 
 @sub_category)) OR
 ((a.FolioNo = @folio_no AND b.ReservationNo = @reservation_id AND 
 @trans_code = 6 AND a.AccountStatementTransCode = 6 AND i.category_id = 
 @sub_category))

I want that if input is @sub_category = 14 then it will return the category with NULL values, else it will return the @sub_category values. How can I do that ?

like image 718
GGw Avatar asked Sep 04 '17 06:09

GGw


People also ask

Can you use parentheses in SQL WHERE clause?

SQL: Using Parentheses with And / OR condition is necessary with where clause to gives expected result.

Can I have WHERE clause in case statement?

Another way to use the Case Statement is within the WHERE clause. There, it may be utilized to alter the data fetched by a query based on a condition. Within that context, the Case Statement is ideally suited to both static queries, as well as dynamic ones, such as those that you would find inside a stored procedure.

Do parentheses work in SQL?

When writing SQL, using parentheses can also be a useful way to distinguish the order of operations and is very complementary to the AND and OR operators. Let's look at an example. We first start with a WHERE clause that has multiple conditions. In this example, we get two results.

How do you do parentheses in SQL?

Can be a single quotation mark ( ' ), a left or right bracket ( [] ), a double quotation mark ( " ), a left or right parenthesis ( () ), a greater than or less than sign ( >< ), a left or right brace ( {} ) or a backtick ( ` ).


2 Answers

Replace CASE expression logic with below

AND 
1 = CASE WHEN @sub_category = 14 AND i.category_id is null 
         THEN 1
         WHEN @sub_category <> 14 AND i.category_id = @sub_category 
         THEN 1
         ELSE 0
    END 
like image 131
Jaydip Jadhav Avatar answered Sep 24 '22 14:09

Jaydip Jadhav


Change your CASE to:

case
    when @sub_category = 14 then null
    else i.category_id = @sub_category 
 end
like image 28
dbajtr Avatar answered Sep 23 '22 14:09

dbajtr