AVG function DB2 SQL run time error - sql

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

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.

SQL Server : CASE statement varbinary comparison has unexpected behavior

UPDATE:
It appears there may be some certificate or rule running on open queries with enrypted column data. I have discovered that the following produces an unencrypted value concat 'more'. I will have to verify with our DBA what may cause this behavior.
case when s.EncryptedColumn is not null then concat(s.[EncryptedColumn], ' more') else s.[RawColumn] end
I am trying to do a simple comparison to null against a varbinary(16) column however I cannot get the result to return true.
This is what I have tried:
Attempt 1:
select
s.[EncryptedColumn],
(case when s.[EncryptedColumn] is not null
then s.[EncryptedColumn]
else s.[RawColumn]
end) as 'result'
Result 1: encrypted data, raw data
Attempt 2:
select
datalength(s.[EncryptedColumn]),
(case when datalength(s.[EncryptedColumn]) > 0
then s.[EncryptedColumn]
else s.[RawColumn]
end) as 'result'
Result 2: encrypted data length (16), raw data
Any ideas?
If I got it right, you should take a look here:
use ISNULL(DATALENGTH(Content), -1) instead, so that you can
distinguish between length 0 and NULL. Or just use DATALENGTH(Content)
UPDATE:
It appears there may be some certificate or rule running on open queries with enrypted column data. I have discovered that the following produces an unencrypted value concat 'more'. I will have to verify with our DBA what may cause this behavior.
case when s.EncryptedColumn is not null then concat(s.[EncryptedColumn], ' more') else s.[RawColumn] end

Updating with case in SQL Server 2008 R2

I want to update a column according to another column value.
for example, In Value column i have numbers between 0 to 1.
I want to check values and if:
Values < 0.45 set ValueStatus=Bad
Values >=0.45 and values<0.55 set ValueStatus =SoSo
Values >= 0.55 set ValueStatus=Good
I wrote the query like this:
update table
set ValueStatus=(case
when Values<'0.45' then 'Bad'
when (Values>='0.45' and Values<'0.55') then 'SoSo'
when Values>='0.55' then 'Good'
else Values
end)
But i get this error :
Error converting data type varchar to float.
Type of Values is Float and ValueStatus is Nvarchar(50)
Thanks
try this (you were adding ' to the numbers and SQL takes them as varchar) :
update table
set ValueStatus=(case when Values<0.45 then 'Bad'
when Values>=0.45 and Values<0.55 then 'SoSo' when Values>=0.55
then 'Good' else Values end )
I believe your problem is based on how the case statement determines the return type. You can read about it here and here.
The numeric types have a higher precedence than the string types. With the else values, you have four clauses in the `case. Three return strings; one returns a number. The number trumps the types so it tries to turn everything into a number.
You can mimic this problem with:
select (case when 1=1 then 'abc' else 12.3 end)
Happily, you can fix this by removing the else clause which is not needed in this case.

Excel to SQL Case

I am trying to convert a really convoluted EXCEL sheet into SQL stored procedure. There is one last formula that is kicking my butt though:
=IF(AND (W4+H4<=0,IF(E4-H4-S4>0,E4-H4-S4,0)<=0) ,0, IF(W4+H4<IF(E4-H4-S4>0,E4-H4-S4,0) ,IF(W4+H4<0,0,W4+H4),IF (E4-H4-S4>0, IF(W4>H4,E4-S4,H4+AF4) ,H4)))
I have created the equations in SQL to sort out the following:
W4.value1
H4.value2
E4_H4_S4.value3
AF4.value4
(as those were already determined earlier in the stored procedure. So I just put them back in as Outer applies for this specific subquery)
However, I have no real idea how to read this Excel formula, and the person that wrote it was fired, so I can't go to her for help.
To me, it reads like this:
=IF(AND (W4+H4<=0,IF(E4-H4-S4>0,E4-H4-S4,0)<=0),0, IF(W4+H4< (IF(E4-H4-S4>0 then E4-H4-S4 else 0) then (IF(W4+H4<0 then 0 else W4+H4) then (IF(E4-H4-S4>0, then (IF(W4>H4 then E4-S4 else H4+AF4) else H4)) )))))
However, I doubt this is correct, as I suck at EXCEL
Anyone able to help me decipher this?
I cannot test this currently, but I think you want something like this:
CASE
WHEN W4+H4<=0 AND (CASE WHEN E4-H4-S4>0 THEN E4-H4-S4 ELSE 0 END)<=0
THEN 0
ELSE
WHEN W4+H4< (CASE WHEN E4-H4-S4>0 THEN E4-H4-S4 ELSE 0 END)
THEN (CASE WHEN W4+H4<0 THEN 0 ELSE W4+H4 END)
ELSE (CASE WHEN E4-H4-S4>0 THEN (CASE WHEN W4>H4 THEN E4-S4 ELSE H4+AF4 END) ELSE H4 END)
END
I really had no idea how to format this, but I believe everything should work as intended according to the ridiculous IF() function, of course substituting your converted values and so on.
EDIT:
This is a straight conversion from EXCEL to SQL so please for the love of programming fix this so 1. someone else can read it (i.e. what if you got fired and you left the next poor soul the same thing you got stuck with) and 2. get rid of the unnecessary checks.

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?