Where clause depending of the value of a variable - sql

I have 2 fields I want to send values to within a WHERE statement.
If a variable = 0 then set 2 field values to 100.
If the variable = 1 then set those same 2 field values to 101.
In my imaginary world, somehow this would work:
Where CASE WHEN #ReportType = 0 THEN
od.StatusCd = 100 AND odm.StatusCd = 100
WHEN #ReportType = 1 THEN
od.statusCd = 101 AND odm.StatusCd = 101
End
And od.CompletionDate between ....
And so on....
I know this is wrong. But this is where I am at right now.

If I have understood what you are trying to do, this should work :
Where
(
(#ReportType = 0 AND od.StatusCd = 100 AND odm.StatusCd = 100)
OR
(#ReportType = 1 AND od.statusCd = 101 AND odm.StatusCd = 101)
)
And od.CompletionDate between ....
And so on....

Alternatively you could rewrite your CASE conditions in the form of a join, like below:
...
INNER JOIN
(
VALUES (0, 100, 100), (1, 101, 101)
) AS v (ReportType, odStatusCd, odmStatusCd)
ON
#ReportType = v.ReportType
AND od.statusCd = v.odStatusCd
AND odm.StatusCd = v.odmStatusCd
WHERE
od.CompletionDate between ...
AND ...
Although somewhat less readable, this would avoid using OR and thus might result in a better (more efficient) execution plan. (You would need to test that.)

You could also just do
WHERE od.StatusCd = 100 + #ReportType AND odm.StatusCd = 100 + #ReportType
Or you could declare a new variable with the value in:
DECLARE #StatusCd = 100 + #ReportType
Keep it simple.

Related

Need to verify value in binary digit in T-SQL

Here is my problem. For example I have a binary digit 0010010 and I only need to verify that third value, from left to right, is 1. How do I do that in T-SQL ?
-- Addded estimated hours field
SET #EstHours = (SELECT SUM(ESTHOURS)
FROM LABORMP
WHERE #PMID = LABORMP.PMID AND ( LEFT(CYCLETYPE,3) = 1 OR LABORMP.CYCLETYPE is null)
)
Use substring() to get the 3d digit and convert to bit, which will return True or False:
declare #binary varchar(8) = '00100010'
select CONVERT(bit, substring(#binary, 3, 1))
returns
True --1
and
declare #binary varchar(8) = '00000010'
select CONVERT(bit, substring(#binary, 3, 1))
returns
False --0
Edit:
For your query you just need:
...SUBSTRING(CYCLETYPE, 3, 1) = '1' ...
if CYCLETYPE is a string.
Your query's logic can be writen like this:
SET #EstHours = (
SELECT SUM(ESTHOURS)
FROM LABORMP
WHERE
#PMID = PMID
AND
SUBSTRING(COALESCE(CYCLETYPE, '111'), 3, 1) = '1'
)
If the value is varbinary, you can use the bitwise and operator (&):
DECLARE #binaryValue varbinary(7) = 0010010;
SELECT 1
WHERE #binaryValue & 0010000 = 0010000
Try this:
SELECT 1
FROM yourtable
WHERE SUBSTRING(yourcolumn, 3, 1) = 1
You will get an empty result if the third digit is not 1

SQL input value either single or multiple values

In SQL query if the input value is 0 then take input as all values of the column else consider input value only;
Let's say if the input city_num = 0 then run the query for all city numbers i.e 1 to 50 else run the query for the input city_num say 5.. How to code this?
if the City_num = 0 then Select * from emp where City_num in 1to 50
if the City_num = 5 then Select * from emp where City_num = 5
;
Add this WHERE statement:
WHERE city_num = ? OR 0 = ?
If you pass 0 then it is equivalent to:
WHERE city_num = 0 OR 0 = 0
and since 0 = 0 is always TRUE then it will fetch all the rows.
If you pass 5 then it is equivalent to:
WHERE city_num = 5 OR 0 = 5
and since 0 = 5 is always FALSE then it will fetch only the row for city_num = 5.
Try This:
DECLARE #input INT = 2;
SELECT * FROM TABLE1
WHERE (city_num= #input AND #input > 0) OR #input = 0;
I have assumed city_num is integer. Minor fix if varchar

Update Zero to One and One to Zero in SQL Server

I have a table with flag 0 and 1.
Please tell me how to update Zero to One and One to Zero
DECLARE #a INT, #b INT
SELECT #a = number
FROM zerone
WHERE number = 0
SELECT #b = number
FROM zerone
WHERE number = 1
BEGIN
IF #a = 0
UPDATE zerone
SET number = 1
WHERE #a = 0
ELSE IF #b = 1
UPDATE zerone
SET number = 0
WHERE #b = 1
END
This query is not working for me.
use the below query to update 0 to 1 and 1 to 0
update zerone set number= 1-number
Just this:
UPDATE zerone
SET number = CASE WHEN number = 1 THEN 0 ELSE 1 END;
you should also always consider NULL values in update operations and must explicitly filter them out
UPDATE zerone
SET number= case
when 1 then 0
when 0 then 1
end
WHERE number in (0,1)
Also your query will not work because you are assigning a single variable with a rows of data in the select statement.
SELECT #a = number
FROM zerone
WHERE number = 0
This does not work like as you are expecting and will only assign #a with value either NULL(if there are no rows) or with 0 if there is a row. If there are multiple rows even then it will have a single value 0
As you have seen there are so many ways to perform that task but in this case, I would prefer #Abdul Rasheed.
We can also use below IIF logical function in SQLSERVER2012 or above.
UPDATE zerone
SET number = IIF (number = 1, 0, 1)

Optimise Stored Procedure Update and Select

The stored procedure I've created works, but is there a more efficient way of doing this? To be fair there is no performance hit for any of this nor does it need optimised but I'd like to know in the interest of doing things correctly.
Execution plan states query 1: 17%, query 2: 67%, query 3: 16%
DECLARE #CurrentVoucherID int;
SET #CurrentVoucherID =
(
SELECT TOP(1) IdGiftVoucherPhysicalCode
from GiftVoucherPhysicalCodes
WHERE Activated = 0 and assigned = 0 and Value = 10
ORDER BY IdGiftVoucherPhysicalCode
);
UPDATE GiftVoucherPhysicalCodes
SET Activated = 1, Activated_at = GETDATE()
WHERE IdGiftVoucherPhysicalCode = #CurrentVoucherID;
SELECT * FROM GiftVoucherPhysicalCodes
WHERE IdGiftVoucherPhysicalCode = #CurrentVoucherID;
I may not be understanding it correctly, but it looks like you are just running the update one record at a time? Why not do it in bulk?
UPDATE GiftVoucherPhysicalCodes
SET Activated = 1, Activated_at = GETDATE()
WHERE Activated = 0 and assigned = 0 and Value = 10
you can do it without variable
UPDATE GiftVoucherPhysicalCodes
SET Activated = 1, Activated_at = GETDATE()
WHERE IdGiftVoucherPhysicalCode = (SELECT TOP(1) IdGiftVoucherPhysicalCode
from GiftVoucherPhysicalCodes
WHERE Activated = 0 and assigned = 0 and Value = 10
ORDER BY IdGiftVoucherPhysicalCode)
SELECT * FROM GiftVoucherPhysicalCodes
WHERE IdGiftVoucherPhysicalCode = (SELECT TOP(1) IdGiftVoucherPhysicalCode
from GiftVoucherPhysicalCodes
WHERE Activated = 1 and assigned = 0 and Value = 10
ORDER BY IdGiftVoucherPhysicalCode)

Where and if clause in SQL query

In a stored procedure, let's say I have a flag, which can be either 0 or 1
If 1, then I want to select * from table A where name = 'blah'.
If 0, then I would want to select * from table A where name = blah and age = 13.
Is there a way I can add and age = 13 to a stored procedure query?
This is what I have currently.
IF #flag = 1
SELECT * from A where name = 'blah'
ELSE
SELECT * from A where name = 'blah' and age = 13
I would like to know for cases where query becomes really long, and so copying and pasting with few more likes for ELSE case is very inefficient.
Thanks.
SELECT * from A
where name = 'blah'
and (#flag = 1 or age = 13)
You can also use a Case statement;
Select * from A
Where Name = 'blah' And Age = Case #flag when 1 then Age else 13 end