Update with case statement syntax issue - tsql

I'm trying to figure out why this syntax is malformed, I do not it:
update Entry
set Name = Case charindex('/', reverse(Name))
when charindex('/', reverse(Name)) > 0 then right(Name, charindex('/', reverse(Name)) -1)
when charindex('/', reverse(Name)) < 0 then Name
End

Try this:
update Entry
set Name = Case
when charindex('/', reverse(Name)) > 0 then right(Name, charindex('/', reverse(Name)) -1)
when charindex('/', reverse(Name)) < 0 then Name
End
EDIT: or this:
update Entry
set Name = Case SIGN(charindex('/', reverse(Name)) )
when 1 then right(Name, charindex('/', reverse(Name)) -1)
when -1 then Name
End
CASE with an expression before WHEN compares that expressison to each expression following WHEN in turn until it finds a match. CASE immediately followed by WHEN checks each expression following WHEN until it finds one that evaluates to TRUE. In the first usage, the expressions must be comparable. In the second, the expressions must evaluate to T/F.

Related

Case when statement conversion failed with column that contains integers and string

Below is what my data looks like for the case when statement:
0
1
NULL
Not Obtainable
NULL
Not Obtainable
2
0
NULL
1
NULL
NULL
Here is the query I am trying to use:
SELECT
*,
CASE
WHEN ER_MENTAL > 1 THEN 'Yes'
WHEN ER_MENTAL < 1 THEN 'No'
WHEN ER_MENTAL = 'Not Obtainable' THEN 'No'
END AS ER_MENTAL_RESP
FROM
#HLQ
This is the error I'm getting:
Conversion failed when converting the varchar value 'Not Obtainable' to data type int.
I've tried various version of CONVERT in front of each case when with no luck
If you need a version that will work on earlier versions of SQL Server then using ISNUMERIC should work:
select er_mental,
case when ISNUMERIC(er_mental) = 1 then
case when er_mental > 1 then 'Yes'
when er_mental < 1 then 'No'
end
when er_mental = 'Not obtainable' then 'No'
end as er_mental_resp
from #hlq;
Your column is VARCHAR so you can't compare it to an INT without converting it. You can use TRY_CONVERT since you are using 2012 which will avoid your error. You could also compare it to it's varchar equivalent, like ER_MENTA > '1' but you could run into issues since varchar's aren't compared the same way. i.e. 11 is < 2 when using varchar. select 1 where '11' < '2'
SELECT *,
CASE WHEN TRY_CONVERT(INT, ER_MENTAL) > 1 THEN 'Yes'
WHEN TRY_CONVERT(INT, ER_MENTAL) < 1 THEN 'No'
WHEN ER_MENTAL = 'Not Obtainable' THEN 'No'
END AS ER_MENTAL_RESP
FROM #HLQ

Determine Group By from Parameter -

How is it that this is valid:
ALTER PROCEDURE [StoredProcedure]
#abcID int = null -- optional param
SELECT columnJ, columnK, Count(eID) AS Num, Sum(OutXYZ) as TotalProdXYZ, Sum(RawXYZ) as TotalRawXYZ
FROM [v_ViewTable]
WHERE (#abcID IS NULL OR (abcID = #abcID))
GROUP BY columnJ, columnK
But then this is throwing a "columnJ is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause."
ALTER PROCEDURE [StoredProcedure]
#abcID int = null -- optional param
-- if 0: Group by columnJ, columnK
-- else: Group by columnK, columnJ
, #Grouping int = null
SELECT columnJ, columnK, Count(eID) AS Num, Sum(OutXYZ) as TotalProdXYZ, Sum(RawXYZ) as TotalRawXYZ
FROM [v_ViewTable]
WHERE (#abcID IS NULL OR (abcID = #abcID))
GROUP BY
CASE WHEN #Grouping = 0 THEN columnJ ELSE columnK END
,CASE WHEN #Grouping = 0 THEN columnK ELSE columnJ END
What's wrong with my CASE statement? Basically, if #Grouping = 0, I want the group by to be J, K if anything else, it should be K, J
Thanks in advance for any help!
As the message says columnJ and columnK are not contained in either an aggregate function or the GROUP BY clause in the second case. You should use field or the exact expression in select list AS IS in the GROUP BY section.
So the following statement will be ok:
SELECT CASE WHEN #Grouping = 0 THEN columnJ ELSE columnK END,
CASE WHEN #Grouping = 0 THEN columnK ELSE columnJ END,
Count(eID) AS Num, Sum(OutXYZ) as TotalProdXYZ, Sum(RawXYZ) as TotalRawXYZ
FROM [v_ViewTable]
WHERE (#abcID IS NULL OR (abcID = #abcID))
GROUP BY
CASE WHEN #Grouping = 0 THEN columnJ ELSE columnK END
,CASE WHEN #Grouping = 0 THEN columnK ELSE columnJ END

CHARINDEX is returning 0 even when the substring is present

I'm trying to extract a substring of one of the column.
SELECT PT.col1 AS MyColumn
,(
CASE WHEN CHARINDEX(PT.col1, '$') > 0
THEN SUBSTRING(PT.col1, CHARINDEX(PT.col1, '$') + 1,
LEN(PT.col1) - CHARINDEX(PT.col1, '$') - 1)
ELSE ''
END
)
,T1.col2
,PT.col3
,T1.col4
,T1.col5
//...
All I'm getting is empty string for all the rows. I've tried just to display the index of the $' character but I'm getting0`.
The value of PT.col1 looks like French (Canada)$fr-CA, Portuguese (Brazil)$pt-BR, ...
Am I missing something?
You have to switch the parameters:
SELECT CHARINDEX('a$bc', '$') AS Wrong, CHARINDEX('$', 'a$bc') AS Ok
/*
Wrong Ok
----------- -----------
0 2
*/

sql: case when (with in param value)

could you please help?
SELECT
(some columns),
SortOrder = CASE WHEN City = #inParamCity THEN 0 ELSE 1 END
FROM
dbo.addressBook
ORDER BY
SortOrder
I tried this and got:
Incorrect syntax near '=' ' –
SELECT
(some columns)
FROM
dbo.addressBook
ORDER BY
CASE
WHEN City = #inParamCity THEN 0
ELSE 1
END
Shouldn't it be like this?
SELECT (some columns),
CASE City WHEN #inParamCity THEN 0 ELSE 1 END As SortOrder
FROM dbo.addressBook
ORDER BY SortOrder

need to translate specific t-sql case in pl/sql

Can anyone tell me how to translate the following T-SQL statement:
SELECT fileld1 = CASE
WHEN T.option1 THEN -1
ELSE
CASE WHEN T.option2 THEN 0
ELSE 1
END
END
FROM Table1 AS T
The point is I need to validate two different options from the table for a single field in the select statement..
I have tried to do somthing with an IF statement in pl/sql, but it just doesnt work for me:
SELECT IF T.option1 THEN -1
ELSE IF T.option2 THEN 0
ELSE 1
END
FROM Table1 AS T
I am not actually sure how to write IF statement inside the SELECT statement..
And also, I need to do it INSIDE the select statement because I am constructing a view.
Use:
SELECT CASE
WHEN T.option1 = ? THEN -1
WHEN T.option2 = ? THEN 0
ELSE 1
END AS field1
FROM Table1 AS T
I can't get your original TSQL to work - I get:
Msg 4145, Level 15, State 1, Line 4
An expression of non-boolean type specified in a context where a condition is expected, near 'THEN'.
...because there's no value evaluation. If you're checking if the columns are null, you'll need to use:
SELECT CASE
WHEN T.option1 IS NULL THEN -1
WHEN T.option2 IS NULL THEN 0
ELSE 1
END AS field1
FROM Table1 AS T
...or if you need when they are not null:
SELECT CASE
WHEN T.option1 IS NOT NULL THEN -1
WHEN T.option2 IS NOT NULL THEN 0
ELSE 1
END AS field1
FROM Table1 AS T
CASE expressions shortcircuit - if the first WHEN matches, it returns the value & exits handling for that row - so the options afterwards aren't considered.
If I remember correctly, PL/SQL also supports the case. You just would have to move the column alias from "field1=" before the expression to "AS filed1" after the expression.