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 ?
SQL: Using Parentheses with And / OR condition is necessary with where clause to gives expected result.
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.
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.
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 ( ` ).
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
Change your CASE
to:
case
when @sub_category = 14 then null
else i.category_id = @sub_category
end
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With