MSSQL Case denoted by an integer returns a string - sql

I have an issue with my SELECT statement in SSRS.
I want to use an integer to return strings values.
I tried it with this SELECT clause:
SELECT CASE #param = '1' THEN value like '__%' ELSE value like ' '
But it doesn't work, so I tried to use this one instead:
WHERE
((#param = '1' AND value like '__%') OR (#param = '0' AND value = '%'))
The expected result is: When the case is "1" SELECT should return only values which are not ' '
When the case is "0" SELECT should return all values = ' ' + '__%'
Thank you for your help

Your case condition is wrong formatted. You missed WHEN and END .It should be like that:
SELECT CASE WHEN #param = '1' THEN value like '__%' ELSE value like ' ' END
EDIT : As much as I understand you want to see Value if the #param='1' and if not the result will be '__%'. If yes this following CASE usage should be correct:
SELECT CASE WHEN #param = '1' THEN value ELSE value= '__%' END

I fix it my self condition is
((#column = '1' AND column like '__%') OR (#column = '0'AND column like '_%'))

WHERE
((#param = '1' AND value != '') OR (#param = '0'))
just try this

Related

How to remove condition check in the where clause statement?

I would like to know how to use if/else or case statement in the below query or anything that could achieve the purpose.
The idea is that, whenever a keyword is NULL or blank, I don't want to include the pv.AdminPost = 1 and pv.IsStaff = 1 condition in the Where statement.
Is there any way to do it? Thank you.
;WITH cte
AS (SELECT fv.Id, fv.[Description]
FROM FeedView fv
WHERE (fv.Id = #PostId OR fv.[Description] LIKE '%' + #Keyword + '%' OR #Keyword IS NULL ) AND fv.ForUserId = #UserId
UNION
SELECT pv.Id, pv.[Description]
FROM PublicView pv
WHERE #InculdePublicPosts = 1 -- This acts as an off switch to decide whether to include public post or not.
OR pv.AdminPost = 1 OR pv.IsStaff = 1 -- how to remove this condition when Keyword is NULL or empty
AND (pv.Id = #PostId OR pv.[Description] LIKE '%' + #Keyword + '%' OR #keyword IS NULL )
)
SELECT cte.Id, cte.[Description]
FROM .....
You must use proper paranthesis and OR condition as follows:
WHERE #InculdePublicPosts = 1
AND ((#keyword IS NULL OR #keyword = '') OR pv.AdminPost = 1 OR pv.IsStaff = 1)
AND (pv.Id = #PostId OR pv.[Description] LIKE '%' + #Keyword + '%' OR #keyword IS NULL )
You can do it this way
AND (ISNULL(keyword, '') = '' OR (pv.AdminPost = 1 OR pv.IsStaff = 1))
Explain:
Using ISNULL() function to get the empty value in case that keyword is null.
If keyword is NULL or blank, then the remaining condition will not run thanks to OR condition. Example:
false or true --> true
true or false --> true
true or true --> true
false or false --> false

I want to concatenate these case statements

I'm getting multiple results from these statements and want to concatenate these. Please help with syntax and structure
(SELECT NVL (
CASE WHEN (csi.hello = DT.S)
THEN 'Task IS ALREADY DONE'
WHEN VV.Mo LIKE NULL
THEN 'The model is not available'
WHEN (B.tsk = '7')
THEN 'The task status has been Cancelled'
WHEN (B.tski = '14')
THEN 'The task has been Assigned'
WHEN (B.tsko = '11001')
THEN 'The task has been Return Part STATUS'
END, '')RMA_CRITERIA,
Use string concatenation:
SELECT (CASE WHEN csi.hello = DT.S THEN 'Task IS ALREADY DONE;' ELSE '' END ||
CASE WHEN VV.Mo IS NULL THEN 'The model is not available;' ELSE '' END ||
CASE WHEN B.tsk = '7' THEN 'The task status has been Cancelled;' ELSE '' END ||
CASE WHEN B.tski = '14' THEN 'The task has been Assigned;' ELSE '' END ||
CASE WHEN B.tsko = '11001' THEN 'The task has been Return Part STATUS;' ELSE '' END
) as RMA_CRITERIA,
Note that this adds a separator (;), although you do not specify one. Also LIKE NULL always returns the equivalent of false; I assume you intend IS NULL. And the ELSE '' is redundant in Oracle, but it makes the intention clear.

Using prompts in Select Case statements

I want to use select case for my prompts. Condition is that when the prompt :4 = 'I' then prompt :5 equals all of its values, and I use the code below but i receive the following error.
Error in running query because of SQL Error, Code=936, Message=ORA-00936: missing expression (50,380)
2 = 2 AND (CASE :5
WHEN :4 = 'I' AND :5 = ' '
THEN :5 = '%'
END)
Anything wrong with my case statemnt?
There are two different syntaxes for CASE:
CASE value1
WHEN value2 THEN expression1
WHEN value2 THEN expression2
ELSE expression3
END
and:
CASE
WHEN boolean_expression THEN expression1
WHEN boolean_expression THEN expression2
ELSE expression3
END
Note: The first statement can be converted to the second as
CASE
WHEN value1 = value2 THEN expression1
WHEN value1 = value3 THEN expression2
ELSE expression3
END
You are mixing these two syntaxes and it is invalid SQL.
You appear to want:
:5 = CASE WHEN :4 = 'I' AND :5 = ' ' THEN '%' END
However, even that will not work as bind variables are set once and are not re-evaluated so your logic would be:
( ( :4 = 'I' AND :5 = ' ' ) AND :5 = '%' )
OR ( NOT ( :4 = 'I' AND :5 = ' ' ) AND :5 = NULL )
Since :5 cannot ever be both ' ' and '%' then that branch of the logic can never be true and anything (including NULL) is never equal to NULL so the second branch of the logic is also never true; therefore your expression will never match anything.
Just create an expression like this
decode(:4,'I',:5,SOMEVALUEORFIELD) = :5
This is your case expression:
(CASE :5 WHEN :4 = 'I' AND :5 = ' ' THEN :5 = '%'
END)
I cannot figure out what you really want. Both the CASE :5 and the :5 = '%' are improper. Perhaps:
(CASE WHEN :4 = 'I' AND :5 = ' ' THEN '%'
END) as :5
However, you don't normally use parameters for column aliases.

Optional where clause depending on parameter value

I want the where statement for this query to be optional depending on what the value of parameter #RetrieveAll is. If #RetrieveAll is false/null the where statement is used, if it is true it should be ignored.
#IncludeErrors bit = 1,
#IncludeAccess bit = 1,
#IncludeLogins bit = 1,
#RetrieveAll bit = NULL
SELECT
//...
FROM
(
) AS a
WHERE
a.RowNumber BETWEEN #ItemCountStart AND #ItemCountEnd
Is there way to do this?
SELECT
//...
FROM
(
) AS a
WHERE
(a.RowNumber BETWEEN #ItemCountStart AND #ItemCountEnd AND #RetrieveAll IS NULL)
OR
#RetrieveAll IS NOT NULL
You should try this,
Select * from Tablename a
Where
1 = case when isnull(#RetriveAll,0) == 0 then
case when a.RowNumber BETWEEN #ItemCountStart AND #ItemCountEnd then 1 else 0 end
else 1 end

How to Replace null with value when null

I have a join query, I want Replace a NULL value with a string if is null.
If it is not NULL, replace with a null string.
select distinct Code= R_Schedule.FoodId,Name= FoodName + ' = price ' +(CONVERT( varchar(50),R_Foods.FoodPrice) + ISNULL(str(R_Schedule.dayId ),' - public-')) from R_Foods join R_Schedule on R_Foods.FoodId=R_Schedule.FoodId where R_Schedule.DayId=6 or DayId is null
please this this Image :
http://i.stack.imgur.com/Kv81f.png
Important section of my query is :
ISNULL(str(R_Schedule.dayId ),' - public -'))
This section if it is null return 'public' else if it is not null return column's id.
I don't want return id if it is not null.
You can use the CASE WHEN. When dayId is null then it will return - public- Else it will concat with '' Empty string
which you can use the other value to set if you want.
select distinct
Code = R_Schedule.FoodId,
Name = CASE WHEN R_Schedule.dayId IS NULL THEN FoodName + ' = price ' +(CONVERT( varchar(50),R_Foods.FoodPrice)) + ' - public-'
ELSE FoodName + ' = price ' +(CONVERT( varchar(50),R_Foods.FoodPrice)) + 'othervalue' END
from
R_Foods join R_Schedule
on R_Foods.FoodId=R_Schedule.FoodId
where
R_Schedule.DayId=6
or DayId is null