Invalid character found in string argument 'DECFLOAT' - sql

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

Related

select case when number then char in postgresql

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.

An expression of non-boolean type

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

ORA-00932 inconsistent datatypes expected char got number

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?

Does SQL Server really evaluate every 'then' clause in a case expression?

I'm working with an ItemNumber field in a legacy system that is 99% numbers, but there are a few records that contain letters. The numbers are all padded with leading zeros so I thought I would just cast them as bigint's to solve this problem, but of course it throws an error when it gets to the records with letters in them.
I thought the following case statement would have worked, but it still throws the error. Why in the world is SQL Server evaluating the cast if the isnumeric(itemnumber) = 1 condition isn't true?
select case when isnumeric(itemnumber) = 1
then cast(itemnumber as bigint)
else itemnumber
end ItemNumber
from items
And what's the best workaround?
Your expression tries to convert a VARCHAR value into a BIGINT if it's numeric and leave the value as is if it's not.
Since you are mixing datatypes in the CASE statement, SQL Server tries to cast them all into BIGINT but fails on non-numeric values.
If you just want to omit non-numeric values, get rid of the ELSE clause:
SELECT CASE ISNUMERIC(itemnumber)
WHEN 1 THEN
CAST(itemnumber AS BIGINT)
END
FROM items
Maybe because:
ISNUMERIC returns 1 for some characters that are not numbers, such as plus (+), minus (-), and valid currency symbols such as the dollar sign ($). For a complete list of currency symbols, see money and smallmoney (Transact-SQL).
http://msdn.microsoft.com/en-us/library/ms186272.aspx
The problem is that you are still mixing your types:
select case when isnumeric(itemnumber) = 1
then cast(itemnumber as bigint) --bigint
else itemnumber --varchar or whatever
end ItemNumber --????
from items
You need two columns
select case when isnumeric(itemnumber) = 1
then cast(itemnumber as bigint) --bigint
else -1
end NumericItemNumber
from items
select case when isnumeric(itemnumber) = 1
then ''
else itemnumber
end StringItemNumber
from items
Then you need to build a query that takes both ints and varchars

SQL CASE in Query - Odd behavior

Using a Case Statement in a query seems to work fine as long as the only case you want to match to is a string. But if you want to put ">" greater than (date or number), etc, SSMS automatically adds apostrophes around your statement (below) as if it were a string.
How can I get it to accept "greater than date" or "greater than number", etc??
There are two types of CASE expression:
The simple CASE expression compares an expression to a set of simple expressions to determine the result.
The searched CASE expression evaluates a set of Boolean expressions to determine the result.
You want a searched CASE expression. This means that the WHEN should come immediately after the CASE keyword.
CASE WHEN SoldDate <= SubjectDate THEN ...
There are two types of CASE expressions (from msdn):
A Simple CASE compares the specified expression for EQUIVALENCY ONLY
SELECT FieldName CASE WHEN 'Blah' THEN 'Foo' ELSE 'Bah' END
A Searched CASE can do inequalities too but has more verbose syntax:
SELECT CASE WHEN FieldName >= 'Blah' THEN 'Foo' ELSE 'Bah' END
You are trying to use the Simple syntax and need to use the Searched syntax by explicitly writing out the fieldname and comparison for each evaluation.
Your CASE statement syntax is incorrect. WHEN should come directly after CASE
This:
CASE SoldDate WHEN ...
Should be this:
CASE WHEN SoldDate <= SubjectDate THEN SoldFor ELSE EstSalePrice END
Additional Information
Take a look at the MSDN docs for additional examples of CASE syntax.