I'm trying to make a query that in case detect a number and in then shows me a char but not works. My sentence is:
SELECT xxxx
CASE
WHEN status=11 THEN 'OK'
WHEN status=33 THEN 'BAD'
ELSE status
END,
...............
It gives me an error:
ERROR: invalid input syntax for integer: "OK"
What's wrong? Any help, please?
Thanks and sorry for my English!
You need to convert to a string. Something like this:
(case when status = 11 then 'OK'
when status = 33 then 'BAD'
else status::text
end),
A case expression returns a single value, with a specified type. By the rules of SQL, when you mix strings and numbers, numbers are preferred.
Postgres makes it easy to convert the value you want to a string in the else clause.
Related
I run this sql statemnt in DB2
SELECT (CASE WHEN AVG(TVAPC) > 0 THEN 'Y' ELSE 'N' END)
FROM TABLE1
So far it was working.. TVAPC column is char(5).
But on run time we got that error. sql error +364 error. And we can't catch that error familiar case. Sometimes we get sometimes we cant for same case.
It seems run time error, but how can i prevent that error?
Thank you so much!
I don't have enough reputation to comment your post yet, so I'm going to post an "answer" with the risk of the stackoverflow royalty voting it down and trashing me for doing so. Check the data type of the column. When I run it, it works fine as long as the columns data type is a numeric type. If it happens to be your case (that the col. type isn't numeric) cast/convert it to Int.
--In case it isn't a numeric type...
SELECT (CASE WHEN AVG(CAST(column as int))>0 THEN 'Y' ELSE 'N' END)
FROM TEMP1234
Why are you taking the average of a string? You should fix the data format.
In any case, you can check if the value has a number. Here is one way (assuming the value is a number):
SELECT (CASE WHEN AVG(CASE WHEN LENGTH(TRANSLATE(test_str, '*', ' 0123456789')) = 0 THEN TVAPC END) > 0
THEN 'Y' ELSE 'N'
END)
FROM TABLE1
With this:
select puser_id, puser_name, plast_login_time, plicense_level =
case
when '1' then 'Потребитель'
else 'Автор'
end
from dbo.PPOM_USER
order by plast_login_time
I have error like this:
An expression of non-boolean type specified in a context where a
condition is expected, near 'then'.
Please help me to find the problem in code.
Change your WHEN to:
when plicense_level = '1'
You need to have a valid comparison in WHEN to avoid the error.
From the CASE documentation:
WHEN when_expression
Is a simple expression to which input_expression is compared when the
simple CASE format is used. when_expression is any valid expression.
Your syntax for the CASE statement expression isn't quite right, try:
case plicense_level
when '1' then 'Потребитель'
else 'Автор'
end
If what you're trying is to show the user a text describing the licennse instead of the numeric value of the column you should try something like this:
select
puser_id,
puser_name,
plast_login_time,
case plicense_level
when '1' then 'Потребитель'
else 'Автор'
end as plicense_level
from dbo.PPOM_USER
order by plast_login_time
You can find some samples of CASE in this page at MSDN
Thank all of you!
The dicision was in changing data type of field plicense_level in report
Figure 1
Figure 2
select puser_id, puser_name, plast_login_time, plicense_level =
case
when plicense_level = '1' then 'Потребитель'
else 'Автор'
end
from dbo.PPOM_USER
order by plast_login_time
I seem to be having a problem using with a filed when used in a case statement. For example when I try:
select a.*,
case when value > 0 then non_zero
else value
from mytable a
where I get:
Invalid character found in string argument of the function 'DECFLOAT'
usually I come across this type of error by forgetting to cast the field as decimal, but this makes no difference. However if I try:
select * from mytable where value > 0
then this runs fine.
So why will my criteria not work within the case statement?
I think you forgot your "end case". Try this:
select a.*,
case
when value > 0 then non_zero
else value
end case
from a
When I try to execute the query below I get the error:
ORA-00932 inconsistent datatypes expected char got number
select (case when upper (TEXT) <> lower (TEXT) then 'INVALID'
else sum(TEXT)
end)
from CLASS
where SECTION = 'SEVENTH'
The query works fine when I remove SUM in ELSE condition> But I need to SUM the Text to achieve the expected result.
You can't sum a character value and all the returned values in a CASE statement must be the same datatype.
If you transform your SUM to a character using TO_CHAR() this still won't work as you're not grouping correctly, see this SQL Fiddle.
The easiest way to do this would be to return a 0 rather than 'INVALID', sum over the entire case statement and change this back to 'INVALID' if it's 0. It's not quite the same... don't store numbers in character columns?
select case when a = 0 then 'INVALID'
else to_char(a)
end
from ( select sum( case when upper(txt) <> lower(txt) then 0
else to_number(txt)
end ) as a
from class )
Here's a SQL Fiddle to demonstrate.
Well you can't SUM text, right? That's why it works well when you remove the operation from the CASE. Have you tried using to_number on the value you're trying to sum?
I used a case to change boolean column values to a text string that actually means something. Firstly, when doing a WHERE statement on these columns do I use the boolean values or the updated text strings?
I used:
WHERE TrainingProgram.blnInCatalogue = 'Yes'
AND TrainingProgram.blnActive = 'Inactive'
But I get the error:
Conversion failed when converting the varchar value 'Yes' to data type bit.
Am I doing something wrong?
For true, try using:
WHERE TrainingProgram.blnInCatalogue <> 0
For false, try using:
WHERE TrainingProgram.blnInCatalogue = 0
The blnInCatalogue column is a bit field, and it doesn't understand what the string 'Yes' means. It only knows 0 or 1.
Though you can use a case to display bit columns as readable strings, their internal value is still bit. They can only be compared to 0 or 1.