SQL input value either single or multiple values - sql

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

Related

input parameter used in case statement in where clause

I have the following simplified stored procedure where based on on the input parameter, I need to then do a case in the where clause. It will not execute as it says: Incorrect syntax near '='
PROCEDURE [dbo].[DataInfo]
#Allowactive BIT = 1
AS
BEGIN
Select * from tbl1 1
where (CASE #Allowactive
WHEN 0 then (t.Isactive = 1) END
AND isSubmitted = 1
END
your where clause will be like below
where
CASE #Allowactive
WHEN 0 then t.Isactive END =1
AND isSubmitted = 1
You shouldn't use parameters in a query like this, as it messes up the query plan. When the right plan to use changes depending on the parameter, you need separate queries, or to force SQL to always recompile.
So do this instead:
create or alter procedure [dbo].[DataInfo] #Allowactive bit = 1
as
begin
if #Allowactive = 0
begin
Select * from tbl1 1
where Isactive = 1
AND isSubmitted = 1
end
else
begin
select * from tbl1 1
where isSubmitted = 1
end
end
Instead run separate queries.
Try to run the following and see the results:
SELECT *
FROM Tbl1 AS T
WHERE CASE #Allowactive
WHEN 0 THEN 1 ELSE #Allowactive END = T.Isactive
AND
isSubmitted = 1;
If you have 2012+ version then you could also do:
SELECT *
FROM Tbl1 AS T
WHERE IIF(#Allowactive = 0, 1, #Allowactive) = T.Isactive;
It seems Zaynul Abadin Tuhin directly answers your question.
But, I believe a case statement complicates what you want to achieve.
I think a query like this satisfies your desired outcome:
PROCEDURE [dbo].[DataInfo]
#Allowactive BIT = 1
AS
BEGIN
SELECT * FROM tbl1 t
WHERE (#Allowactive = 1 OR (#Allowactive = 0 AND t.Isactive = 1))
AND t.isSubmitted = 1
END

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)

Where clause depending of the value of a variable

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.

Update columns in sequence

I am busy writing a piece of SQL where I only want to update column 2 if the result from column 1 fits certain criteria.
In the example below I am looking for some kind of syntax which will allow me to execute one update before proceeding to the next so I can use the result from the first in the second.
CREATE TABLE #Rows
(
RowID INT IDENTITY
,Num1 INT
,Num2 INT
)
INSERT INTO #Rows
(
Num1,Num2)
VALUES
(1,10),
(1,10)
SELECT
*
FROM
#Rows
UPDATE
#Rows
SET
Num1 = CASE WHEN Num1 + Num2 < 20 THEN 10 --Update 1 : I want this statement to execute first
END
,Num2 = CASE WHEN Num1 + Num2 = 20 THEN 100 ELSE 700 --Update 2 : I want this statement to execute after Update 1
END
SELECT
*
FROM
#Rows
DROP TABLE #Rows
The result I get is as follows:
RowID Num1 Num2
1 10 700
2 10 700
I am hoping to get the following result:
RowID Num1 Num2
1 10 700
2 10 100
Any ideas?
Since Num1 is set to the result of an expression you can just re-use that expression in the second CASE statement:
UPDATE
#Rows
SET
Num1 = CASE
WHEN Num1 + Num2 < 20 THEN 10
END,
Num2 = CASE
WHEN CASE
WHEN Num1 + Num2 < 20 THEN 10
ELSE Num1
END + Num2 = 20 THEN 100
ELSE 700 --Update 2 : I want this statement to execute after Update 1
END
Yeah it's messy but it should work.
I suppose the alternative is to wrap the whole thing in a transaction and do two updates
I think you cant do that, you will have to two update statements or change the CASE conditions. SQL syntax is not designed for that, as far as I know.
Read more info here:
http://dev.mysql.com/doc/refman/5.0/es/update.html
http://www.postgresql.org/docs/9.1/static/sql-update.html

Use comparison signs inside a sql case statement

I'm looking for a way to build case statements in a sql select query using less than and greater than signs. For example, I want to select a ranking based on a variable:
DECLARE #a INT
SET #a = 0
SELECT CASE
WHEN #a < 3 THEN 0
WHEN #a = 3 THEN 1
WHEN #a > 3 THEN 2
END
I'd like to write it as:
DECLARE #a INT
SET #a = 0
SELECT CASE #a
WHEN < 3 THEN 0
WHEN 3 THEN 1
WHEN > 3 THEN 2
END
...but SQL doesn't let me use the < and > signs in this way. Is there a way that I can do this is SQL 2005, or do I need to use the code like in the first one.
The reason for only wanting the code there once is because it would make the code a lot more readable/maintainable and also because I'm not sure if SQL server will have to run the calculation for each CASE statement.
I'm looking for a VB.NET case statement equivelent:
Select Case i
Case Is < 100
p = 1
Case Is >= 100
p = 2
End Select
Maybe it's not possible in SQL and that's ok, I just want to confirm that.
You can use the SIGN function as
DECLARE #a INT
SET #a = 0
SELECT CASE SIGN(#a - 3)
WHEN -1 THEN 0
WHEN 0 THEN 1
WHEN 1 THEN 2
END
If #a is smaller than 3, then #a - 3 results in a negative int, in which SIGN returns -1.
If #a is 3 or greater, then SIGN returns 0 or 1, respectively.
If the output you want is 0, 1 and 2, then you can simplify even more:
DECLARE #a INT
SET #a = 0
SELECT SIGN(#a - 3) + 1
Using SIGN as suggested by #Jose Rui Santos seems a nice workaround. An alternative could be to assign the expression an alias, use a subselect and test the expression (using its alias) in the outer select:
SELECT
…,
CASE
WHEN expr < 3 THEN …
WHEN expr > 3 THEN …
END AS …
FROM (
SELECT
…,
a complex expression AS expr
FROM …
…
)
SELECT
CASE
WHEN ColumnName >=1 and ColumnName <=1 THEN 'Fail'
WHEN ColumnName >=6 THEN 'Pass'
ELSE 'Test'
END
FROM TableName