Sum using CASE when using Len(column_name) - sql

I am writing a query that counts the number of records for which the character count is less than 11.
SELECT sum(CASE when LEN(Summary) > 11 then 0 else 1) AS [SummaryErrorCnt],
sum(CASE when LEN(ResolutionNotes) >11 then 0 else 1) AS [ResolutionNotesErrorCnt]
FROM dbo.TicketLog
But I get the error
Msg 156, Level 15, State 1, Line 2 Incorrect syntax near the keyword 'FROM'.
I am closing the parenthesis correctly.
What am I doing wrong?

You're missing the END keyword fro the CASE expressions, eg:
CASE when LEN(Summary) > 11 then 0 else 1 END
^^^-- must close CASE with END

Related

Parseexception kw_end missing near 'as'

I am getting parseexception kw_end missing near 'as' error for the below query :
Case
when x=y then
case when g<h then 2 else 0 end
When x=z then
case when i>k then 6 else 0 end
else
case when v=s then 3 else 0 end
end as 'block';
Here i am not getting what's going wrong.... What's the mistake here to get the above error?
Please use backtick (`) around alias name. Single quote(') can act like a string identifier and may not behave properly.
Case
when x=y then
case when g<h then 2 else 0 end
When x=z then
case when i>k then 6 else 0 end
else
case when v=s then 3 else 0 end
end as `block` --backtick enclosed alias

Replace CHARINDEX return number with 1 if greater than 0

I have a query that looks like this:
Select
CHARINDEX('Test_Value',STRING_AGG(FSD.Test_Value_Field, ';')) As My_Return_Value,
FROM QE_CS CS
...
Which returns a value greater than 0 if it's in the aggregated string, however, I just want it to return 1 if the value is greater than 0. I tried using IF like this:
Select
IF CHARINDEX('Test_Value',STRING_AGG(FSD.Test_Value_Field, ';')) > 0 THEN 1 ELSE 0 As My_Return_Value,
FROM QE_CS CS
But this returns:
Incorrect syntax near the keyword 'IF'
Clearly I'm not familiar with using IFs in SQL server - how can I get my expected result?
If statement cannot be used in select statement.
Use case statement like this
Select
CASe when CHARINDEX('Test_Value',STRING_AGG(FSD.Test_Value_Field, ';')) > 0 THEN 1 ELSE 0 end As My_Return_Value,
FROM QE_CS CS
Yet another option is sign()
Example
sign(CHARINDEX('Test_Value',STRING_AGG(FSD.Test_Value_Field, ';')))
Sign() will return -1, 0, 1 or null
You can try this:
Select IIF(CHARINDEX('Test_Value',STRING_AGG(FSD.Test_Value_Field, ';')) > 0, 1, 0) As My_Return_Value FROM QE_CS CS
If you have multiple conditions to be checked CASE is the better option, however you can use 'IIF' function too, official docs here
select IIF(CHARINDEX('Test_Value',STRING_AGG('ABC;Test_Value', ';')) > 0 ,1,0) from QE_CS CS

Oracle Error when dividing by Zero

I have a query where I need to get a certain number divided by 2 fields in 2 columns.
shelve.total_qty/(GREATEST(NVL(y.FORECAST_QUANTITY, 0), NVL(z.sales, 0))/7) as Shelve_DOC
I get the notorious error.
ORA-01476: divisor is equal to zero
01476. 00000 - "divisor is equal to zero"
*Cause:
*Action:
I have read around I need a CASE/IF but I'm not sure how to..
Any help would be appreciated.
SELECT
CASE WHEN (GREATEST(NVL(y.FORECAST_QUANTITY, 0), NVL(z.sales, 0))/7) = 0 THEN null
ELSE shelve.total_qty/(GREATEST(NVL(y.FORECAST_QUANTITY, 0), NVL(z.sales, 0))/7)
END Shelve_DOC
FROM ...
WHERE ....
Should do the trick.
Something like this?
CASE WHEN NVL(y.FORECAST_QUANTITY,0) <= 0 AND NVL(z.sales,0) <= 0
THEN NULL
ELSE shelve.total_qty/(GREATEST(NVL(y.FORECAST_QUANTITY, 0), NVL(z.sales, 0))/7)
END AS Shelve_DOC

Searched Case works / Simple Case doesn't in Oracle

I have the following Searched Case field selection in a Oracle 10g SELECT query
(case
when LOADER_CELLS.CELL_MODE='RW' then 1
when LOADER_CELLS.CELL_MODE='R' then 2
end) as CELL_EDIT_MODE_ID
but if I write it as a Simple Case expression, as follows:
(case LOADER_CELLS.CELL_MODE
when 'RW' then 1
when 'R' then 2
end) as CELL_EDIT_MODE_ID
I get a ORA-12704: character set mismatch error on the when 'RW' line.
I gave a look to the Oracle documentation, and it seems my syntax is correct. http://docs.oracle.com/cd/B19306_01/server.102/b14200/expressions004.htm
Can someone help me on this?
" I supposed that it could be a encoding problem but I don't know how to "cast" the constant strings to a NVARCHAR"
you do it with "N" syntax.
case LOADER_CELLS.CELL_MODE
when n'RW' then 1
when n'R' then 2
end
eg
SQL> select case a when 'a' then 1 end from foo;
select case a when 'a' then 1 end from foo
*
ERROR at line 1:
ORA-12704: character set mismatch
SQL> select case a when n'a' then 1 end from foo;
CASEAWHENN'A'THEN1END
---------------------

Why do I get syntax error Msg 156 when using CASE expression?

I am trying to run this simple query however it doesn't even parse saying
Msg 156, Level 15, State 1, Line 2 Incorrect syntax near the keyword
'between'.
Query is:
select
case DMProject.dbo.TypeFourTraining.Column2
when Column2 between 181 and 360 then '181-360'
when Column2 between 0 and 180 then '0-180'
END as score
from DMProject.dbo.TypeFourTraining
The same doesn't work for Column2 < 360 syntax neither..
I have searched over internet from msdn, and some other sites but I see that my syntax seems to be valid, then either there is a detail I need to know or there is something I can't see :(
Can anyone please suggest a solution?
You cannot mix the simple and searched types of CASE expression. Remove the field name DMProject.dbo.TypeFourTraining.Column2 specified after the CASE.
Correct syntax for your query:
SELECT CASE
WHEN Column2 between 181 AND 360 THEN '181-360'
WHEN Column2 between 0 AND 180 THEN '0-180'
END as score
FROM DMProject.dbo.TypeFourTraining
Two types of CASE expressions:
There are two types of CASE expression namely Simple and Searched. You cannot combine Simple and Searched in the same expression.
Simple CASE:
CASE input
WHEN 1 THEN 'a'
WHEN 2 THEN 'b'
WHEN 3 THEN 'c'
ELSE ''
END
Searched CASE with simple example:
CASE
WHEN input = 1 THEN 'a'
WHEN input = 2 THEN 'b'
WHEN input = 3 THEN 'c'
ELSE ''
END
Searched CASE with slightly complex example:
This involves multiple columns. You can add multiple columns in each of the WHEN statements.
CASEĀ 
WHEN input = 1 AND second_column = 2 THEN 'a'
WHEN input = 2 AND third_column = 3 THEN 'b'
WHEN input = 3 AND (second_column = 4 OR third_column = 6) THEN 'c'
ELSE ''
END
You are mixing up the two forms of the case statement. Try this:
select (case when Column2 between 181 and 360 then '181-360'
when Column2 between 0 and 180 then '0-180'
END) as score
from DMProject.dbo.TypeFourTraining
The other form would be used for singleton values:
select case DMProject.dbo.TypeFourTraining.Column2
when 1 then '1'
when 2 then '2'
etc. etc. etc.
END as score
from DMProject.dbo.TypeFourTraining