TSQL where case - sql

I need the correct tsql syntax for this problem :
Select * from table where var_A='10'
select * from table where var_B='10'
When to use var_A or var_B depends on the value of var_C
So when var_c='something' use var_A='10' else var_B='10'

select *
from [table]
where case when var_c = 'something' then var_a else var_b end = '10'
SQL Fiddle with demo.

Ian probably has the best answer available but I thought I would supply another answer for a different (although less flexible) method in case it helps:
IF var_c = 'something' BEGIN
SELECT * FROM [table] WHERE var_a = '10'
END
ELSE BEGIN
SELECT * FROM [table] WHERE var_b = '10'
END

This how you use case statement within where clause.
select * from table
where ( case when var_c = 'something'
then var_a
else
var_b
end
)

Related

SQL: CASE expression with declared variable

I have following example that doesn't work in SQL server.
table1.field1 is also a char(3)
DECLARE #CALCULATED_FIELD char(3);
SELECT table1.field1
,table1.field2
,CASE
WHEN table1.field2 = '1' THEN #CALCULATED_FIELD = table1.field1
ELSE #CALCULATED_FIELD = 'ALL'
END
FROM table1
Anyone knows the problem with the case statement?
A SQL Server SELECT statement can either assign variable values or return rows, but not both. You might intend:
SELECT #CALCULATED_FIELD = COALESCE(table1.field1, 'ALL')
FROM table1;
This would assign the maximum value to the variable -- and the only value if the table has only one row. If the table has no rows, the variable would be assigned 'ALL'.
The result of the CASE expression must be assigned to the variable, but it is not allowed in the same statement to return any columns from the table.
So this:
SELECT
#CALCULATED_FIELD = CASE
WHEN field2 = '1' THEN field1
ELSE 'ALL'
END
FROM table1
is syntactically valid, although it will assign to the variable finally, the result of the CASE expression of the last row fetched.
By the name of the variable, I suspect that you want a 3d column which will be calculated by CASE:
SELECT field1, field2,
CASE
WHEN field2 = '1' THEN field1
ELSE 'ALL'
END calculated_field
FROM table1
See a simplified demo.
I would use a scaler funtion in this case
CREATE FUNCTION calcfield
(
#field2 VARCHAR(4),
#field1 VARCHAR(4)
)
RETURNS VARCHAR(3)
AS
BEGIN
DECLARE #CalcField CHAR(3)
SELECT #CalcField = CASE
WHEN #field2 = '1' THEN #field1
ELSE 'ALL'
END
RETURN #CalcField
END
GO
and then change your query to
SELECT table1.field1
,table1.field2
,dbo.calcfield(table1.field2,table1.field1)
FROM table1
Expressions like A=B is not a scalar value. So when you run the following query, it fails and you will get an error Incorrect syntax near '='.
SELECT ('aaa' = 'aaa')
In your case
CASE WHEN table1.field2 = '1'
THEN #CALCULATED_FIELD = table1.field1
ELSE #CALCULATED_FIELD = 'ALL'
END
is not a valid expression, and you might mean something like
CASE WHEN table1.field2 = '1'
THEN (CASE WHEN #CALCULATED_FIELD = table1.field1 THEN 1 ELSE 0)
ELSE (CASE WHEN #CALCULATED_FIELD = 'ALL' THEN 1 ELSE 0)
END
Please note that you can run a query like
SELECT columnName = 'aaa'
and it works as
SELECT 'aaa' AS columnName
Thx a lot.
For the create function, I have no rights and we can't do it on the production environment. But would work. Nice.
The example from forpas used and it works perfect for me.

conditional where clause in sql 2012

I have a query as below in my Stored Procedure:
I set the #SUBCONDITION FROM MY CODE BEHIND.
declare #SUBCONDITION VARCHAR(100)= NULL
if(#SUBCONDITION='DEPT')
BEGIN
Select * from table1 where IDDEPT=1
END
ELSE IF (#SUBCONTION='GRADE')
BEGIN
SELECT * FROM TABLE1 WHERE IDGRADE=1
END
ELSE IF(#SUBCONDITION='SECTION')
BEGIN
SELECT * FROM TABLE1 WHERE IDSECTION=1
END
Everything works just fine.
My question is whether I can do this in just one query??? Like using if or case or something in the where clause?
Yes, you can do this in one query. It would look like this:
Select *
from table1
where (#SUBCONDITION = 'DEPT' and IDDEPT = 1) or
(#SUBCONTION = 'GRADE') and IDGRADE = 1) or
(#SUBCONDITION = 'SECTION' and IDSECTION = 1)
Try this:
SELECT *
FROM table1
WHERE 1 = (CASE #SUBCONDITION
WHEN 'DEPT' THEN IDDEPT
WHEN 'GRADE' THEN IDGRADE
WHEN 'SECTION' THEN IDSECTION
ELSE 0
END);
Try this:
Select * from table1
where #SUBCONDITION = 'DEPT' AND IDDEPT=1 OR
#SUBCONDITION = 'GRADE' AND IDGRADE=1
#SUBCONDITION = 'SECTION' AND IDSECTION=1

Select Statement based on user defined variable

I am trying to do a simple select statement based on the input of the user (SSRS). Help!
select * from Table1
WHERE
Case when #x = 'Yes' then (select * from Table1 where [Column1] < 0) end;
case when #x = 'No' then (select * from Table1 where [Column1] > 0) end;
Thank you in advance
KJ
It should be as simple as
select * from Table1
WHERE
(#x = 'Yes' AND [Column1] < 0)
OR
(#x = 'No' AND [Column1] > 0);
By the way, SELECT * is really bad coding, you really should specify each column you are returning.

How do I use an if statement inside the where clause in this sql statement?

select *
from mytable
where (if #key=0
pkey>=0
else
pkey = #key)
#key is the value passed in to the stored procedure and pkey is a column in mytable.
How about this:
select *
from mytable
where ((#key=0 AND pkey>=0) OR (#key<>0 AND pkey = #key))
You use CASE (bit like the way you are trying with if) as below. (DEMO)
select *
from mytable
where pkey = case when #key <> 0 then #key
else Abs(pkey) end
This will work for you . by using case statement you can apply dynamic filter ..
select *
from mytable
where 1 = case
when #key=0 then case when pkey>=0 then 1 else 0 end
else case when pkey = #key then 1 else 0 end
end

Syntax for SQL Server when either of parameter is passed

If one of the paramater is passed / or both passed how to match that to single column in WHERE clause.
If both are passed should get result set just with the first one else with second one.
Ex:
--either will be passed
declare #processStepID int = null
declare #PROCESS_StepID int = null
if(#processStepID is null)
select * from CTL.CTRL.CTRL_PROCESS_STEP_ACTIVITIES
where PROCESS_STEP_ID = #PROCESS_StepID
else
select * from CTL.CTRL.CTRL_PROCESS_STEP_ACTIVITIES
where PROCESS_STEP_ID = #processStepID
How can this be written in where clause not using if statement.
thanks
WHERE PROCESS_STEP_ID = coalesce( #processStepID, #PROCESS_StepID )
Use the ISNULL function
SELECT *
FROM CTL.CTRL.CTRL_PROCESS_STEP_ACTIVITIES
WHERE PROCESS_STEP_ID = ISNULL( #processStepID, #PROCESS_StepID )
select * from CTL.CTRL.CTRL_PROCESS_STEP_ACTIVITIES
where (PROCESS_STEP_ID = #PROCESS_StepID) OR (PROCESS_STEP_ID = #processStepID)
Seems far too obvious. What am I missing?
select *
from CTL.CTRL.CTRL_PROCESS_STEP_ACTIVITIES
where PROCESS_STEP_ID = ISNULL(#PROCESS_StepID,#processStepID)
This should do what you are looking for