Parseexception kw_end missing near 'as' - hive

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

Related

Sum using CASE when using Len(column_name)

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

Use case statement in SQL

I want to change the value in a result using a case statement, but it seems to ignore this. Very simple, if the value in the table is 1 then must change to True, if the value is 0 then must change to false. Here is what I did:
CASE pbc.bShownUp
WHEN 0 THEN 'False'
ELSE pbc.bShownUp
END
AND
CASE ch.bShownUp
WHEN 1 THEN 'True'
ELSE ch.bShownUp
END
No matter what I do, the result is still 0 or 1.
Please help.
When you're using a CASE Expression, the return type of the data has to be the same for all WHEN parts or more simply put, CASE must resolve to a single type.
More reference here and here
From a Type point of view, at the moment your code says
CASE BIT
WHEN 1 then NVARCHAR
ELSE BIT
And that won't work.
So you have to do something like this
CASE ch.bShownUp
WHEN 1 then 'TRUE'
ELSE 'FALSE'
If bShownUp is numeric column, you are producing mixed result - sometimes 0/1, sometimes 'False'/'True' e.g. string. I suppose bShownUp is of type VARCHAR and you have to change WHEN 0 to WHEN '0'.
If your bShownUp is VARCHAR then your query should look like
CASE pbc.bShownUp
WHEN '0' THEN 'False'
ELSE pbc.bShownUp
END
AND
CASE ch.bShownUp
WHEN '1' THEN 'True'
ELSE ch.bShownUp
END
OR
Try something like below,
CASE pbc.bShownUp
WHEN 0 THEN 'False'
ELSE CAST(pbc.bShownUp AS VARCHAR(10))
END
AND
CASE ch.bShownUp
WHEN 1 THEN 'True'
ELSE CAST(ch.bShownUp AS VARCHAR(10))
END
I think this will do:
case ch.bShownUp
when 1 then 'True'
else 'False'
end

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
---------------------

SQL CASE statement returns error

Hi I have select CASE statement in DB2:
case when actualfinish is not null then dec (( timestampdiff(
4,
char(actualfinish - reportdate))/60.00),10,2)
else 'not'
end
It returns me error.
If I delete ELSE part there is no error and calculation is done.
case when actualfinish is not null then dec (( timestampdiff(
4,
char(actualfinish - reportdate))/60.00),10,2)
end
If I change THEN part to something like
case when actualfinish is not null then 'Yes'
else 'not'
end
also there is no error.
But for my complete SQL query - can't find an error.
Thanks
Issue here is that dec function returns a decimal datatype in the else part of the CASE statement you are returning a VARCHAR, hence the issue.
In your second version
case when actualfinish is not null then 'Yes'
else 'not'
end
Both the when and else are returning same datatype hence the query runs fine.
Change your else to send a decimal equivalent value and it should execute fine.
Update:
Use this version if you can return decimal as a string:
CASE
WHEN actualfinish IS NOT NULL THEN CHAR(DEC((TIMESTAMPDIFF(4, CHAR(actualfinish - reportdate))/60.00),10,2))
ELSE 'not'
END

How to write case in sql server?

In sqlserver, I write a query in that i use "case" but it is giving error, this is my case.
(case when sm.SegCode =0 then '' else sm.SegCode = 7 end)
please help me.
Thanks for all for giving response, actually I have a parameter #id. Now I want to check when it is not zero I check that condition sm.segcode else if #id is zero then I don't want to check the condition that is sm.segcode = #id.
There are two issues with the statement you showed:
the syntactic error of else sm.SegCode = 7
the attempted mixing of types with the empty string and the int
Try this instead:
case when sm.SegCode = 0 then '' else '7' end
Of course that is partially a guess, because I'm not sure exactly what you are trying to achieve by setting the result to either an empty string or the integer 7.
this would be syntactically correct:
case sm.SegCode when '0' then '' else '7' end
or
case sm.SegCode when 0 then NULL else 7 end
if that columns allows NULL's that is
you can see more about case when in the documentation: http://msdn.microsoft.com/en-us/library/ms181765.aspx
Always try to return Same DATA TYPE value from Case statement, seems your first condistion returns VARCHAR whereas the else section returns INT, it's not possible, so best to convert each value to VARCHAR, so 7 will be '7' in else statement, thanks
(CASE
WHEN sm.SegCode = 0 THEN ''
ELSE '7' END)