SQL Server add condition dynamically using case - sql-server

In my SQL Server stored procedure, I want to add a Where clause as dynamic as follows:
AND CASE WHEN #mySt <> '' THEN s.myStatus IN(#mySt) ELSE 0=0 END
When I want to check is: if #mySt is not empty, I want to add s.myStatus IN(#mySt).
If if #mySt is empty, I want to skip this where clause.
I get an error
Incorrect syntax near 'IN'

CASE is an expression, it returns a scalar value not a boolean result; you can't do want you're trying to do as s.myStatus IN(#mySt) isn't a scalar value.
Use proper boolean logic:
AND (s.myStatus = #mySt OR #mySt IS NULL) --I suggest NULL over a blank string
I replaced IN with =, as #mySt is a scalar value, so IN and = would be synonyms.

Related

Apply If condition in solr fieldlist

I need a variable value, which will compute the value on the basis of condition I have used.
I need to apply if condition and in case that condition exists I need to return the value as 1 else 0
My logic is:
{!field f=temp_close_timing v='[2022-03-11T11:14:39.667679 TO 2022-03-11T11:14:39.667679]'}
If we get the value from the above statement, we need to return true else return false.
I am not sure how to do this. I have tried exists condition here but I am not sure how to right the same in FL
eg:
fl=if((exists({!field f=temp_close_timing v='[2022-03-11T11:14:39.667679 TO 2022-03-11T11:14:39.667679]'}),1,0),1,0)
but here I am getting error as "Error parsing fieldname: Expected identifier at pos 3 str="
I am not sure how to assign variable and if I assign variable that should be indexed separately.

What's the meaning of this simple SQL statement?

I am new to T-SQL. What is the meaning of the following statement?
BEGIN
UPDATE table_name
SET a = ISNULL(#f_flag,0)
END
Begin, End: The Begin and End is not needed. It identifies a code
block, usefull if more that one statement.
UPDATE table_name: Update the data in the table "table_name".
SET: Keyword, start the comma delimited list of column - value pairs
to update
a = : a is the column mame, to value to the right of the = is what
value will be used
ISNULL(#f_flag,0): The value to assign. In this case the IsNull checks the value of the #f_flag variable, and if it is null, then use a 0.
*Note: that there is no "WHERE" clause here, therefore, all rows in the table will be updated.

Using CHARINDEX and SUBSTRING in a CASE statement

I need to evaluate a field with a CASE statement. The field name is Commodity and is a varchar(255). The values differ and I need to extract a specific portion from it after a specific character. The character is a '>'. I was able to come up with the value I want returned after the > by using the following code:
SUBSTRING(Commodity, CHARINDEX('>', Commodity) + 2, LEN(Commodity))
I am however unsure of how to work this into my CASE statement. I need to test for Is Null and then just assign it a value of 'No Commodity'. Then I need to test for the presence of a > and then implement the code above to return the value. Then I need to test for when there is no > but it is not null and just return the value of the Commodity field.
You just need to have these three conditions in when clauses. You can use charindex to make sure the > character exists in the string:
CASE
WHEN commodity IS NULL THEN 'No Comodity'
WHEN CHARINDEX('>', Commodity) > 0 THEN
SUBSTRING(commodity, CHARINDEX('>', commodity) + 2, LEN(commodity))
ELSE comodity
END

Syntax error when assigning column value using CASE WHEN in Computed Column Formula

I'm trying to use the following CASE WHEN in computed column alias, but it shows syntax error.
[Password_Last_Changed] [datetime] AS
SELECT CASE
WHEN ([SUA_History1_Date] IS NOT NULL) then [SUA_History1_Date]
WHEN ([SUA_History1_Date] IS NULL) then [SUA_History2_Date]
WHEN ([SUA_History2_Date] IS NULL) then [SUA_History3_Date]
WHEN ([SUA_History3_Date] IS NULL) then [SUA_History4_Date]
WHEN ([SUA_History4_Date] IS NULL) then [SUA_History5_Date]
ELSE NULL
END
Not sure what went wrong. If there is a better approach for this logic, please let me try it.
Using COALESCE is better option than ISNULL or CASE..WHEN for this problem since the input values for the COALESCE expression can be evaluated multiple times.
You can also use NULLIF to check the conditions in those NULL valued columns.
A NULL value for ISNULL is converted to int datatype whereas for COALESCE, you must provide a data type. ISNULL takes only 2 parameters whereas COALESCE takes a variable number of parameters.
You can use COALESCE:
COALESCE([SUA_History1_Date],[SUA_History2_Date],[SUA_History3_Date],[SUA_History4_Date],[SUA_History5_Date])
Evaluates the arguments in order and returns the current value of the first expression that initially does not evaluate to NULL.

Boolean expression in SQL

I have two ints: #RecurremceTemp and #WeekDifference
The following line errors:
PRINT #WeekDifference >= #RecurrenceTemp
with Incorrect syntax near '>'
And won't execute.
Can someone please tell me how to write such a boolean expression to include in a Select statement i.e.:
Select *
FROM TableX
WHERE somevariable = x
and #WeekDifference >= #RecurrenceTemp
Predicates cannot be used in expressions, only in IF and WHILE and CASE WHEN:
PRINT CASE WHEN #WeekDifference >= #RecurrenceTemp THEN 1 ELSE 0 END
Your query ought to work just fine.
The reason that the print statement fails is due to the fact that SQL Server treats the results of a boolean expressions very differently than other data types. See Operators:
Unlike other SQL Server data types, a
Boolean data type cannot be specified
as the data type of a table column or
variable, and cannot be returned in a
result set.
Return -1, 0, or 1 from a CASE statement, depending on the comparison. In SQL Sevrer, there is no boolean data type. The nearest data type is BIT, which can have a value of 1, 0 or null.
EDIT:
Your SQL statement
Select *
FROM TableX
WHERE somevariable = x
and #WeekDifference >= #RecurrenceTemp
will select all fields from TableX where somevariable = x and the WeekDifference variable value is greater than or equal to the RecurrenceTemp variable value. Is that not what you want?

Resources