type cast in Case statement - sql

Segment
1
2
3
4
NUll
5
I want to impute 'Other' if the Segment value is null
expected output
Segment
1
2
3
4
Other
5
i have tried
select
case when segment is null then 'Other' else segment end as segment
from table;
It says invalid input syntax for type "numeric":Other

The case expression returns a single type. The problem is that segment is a number, but 'Other' is a string. The expression has to make a choice, and it chooses the numeric type (following standard SQL rules).
This is simple to fix. Just cast segment:
select (case when segment is null then 'Other' else segment::text end) as segment
from table;
It would be more natural to write this query using coalesce():
select coalesce(segment::text, 'Other') as segment
from table;

select
case when CAST(segment AS CHAR) IS NULL then 'Other' else CAST(segment AS CHAR) end as segment
from table

Related

SQL case statement error handling when there are non-integer values

I have a field of 5 digit codes, and I am trying to create a new flag field if the 5 digit code is between 2 numbers. That part is easy, but there are also a lot of values that have letters and aren't strictly 5 digits. So I'm trying to put a statement at the beginning of the case statement that says if there's an error then set the flag to zero. Or a statement that says if the value is not a number then set to zero.
Here's a sample of listed values:
36569
38206
J8502
JAA8C
Here is some code I've tried (simplified to get the point across):
case
when not isnumeric([code]) then 'N'
when [code] between 50000 and 50005 then 'Y'
ELSE 'N'
end as NewFlag
Thanks!
How about using try_cast() or try_convert():
(case when try_convert(int, code) between 50000 and 50005
then 'Y' else 'N'
end) as newFlag
Actually for your particular values, you can do the comparison as strings:
(case when code between '50000' and '50005'
then 'Y' else 'N'
end) as newFlag
This is really a special case, because you have 5 digit codes and you are only concerned about the last character.

Is there boolean type for column in Oracle SQL?

I tried to:
select 1>2 from dual;
but got:
ORA-00923: FROM keyword not found where expected
Is there boolean type for column expression in Oracle SQL?
I able to do:
select case when 1>2 then 'T' else 'F' end from dual;
Originally I tried to compare date fields and the quickest way I found was getting difference and look to sign...
UPDATE I tried SIGN function, I don't know if it is vendor specific extension:
select SIGN(1-2) from dual;
select SIGN(DATE '2017-01-02' - DATE '2017-02-12') from dual;
but this trick doesn't work for strings...
No there is not, you can use 0 and 1 just as yes/no.
If you need to get the result 1 if something is true and 0 if it is false, you can use a case expression:
select case when (any_logical_condition_here) then 1 else 0 end as my_col
from ....
where ....
For example:
select case when 1 > 2 then 1 else 0 end as bool_result
from dual;
BOOL_RESULT
---------------------------------------
0
NOTE though - "Boolean" refers strictly to the TRUE/FALSE logic, it has no place for UNKNOWN. When you deal with null, as you must in SQL, you need three-valued logic. The case expression as written above returns 1 when the logical condition is true and 0 otherwise. Try it with 1 > null - the truth value is UNKNOWN, the case expression will return 0.

Sql ISNUMERIC()

SELECT CASE WHEN ISNUMERIC('1a') =1 THEN 1 ELSE 'A' END
i'am getting this error !!
Conversion failed when converting the varchar value 'A' to data type int.
you are a victim of data type precedence.Taken from BOL:
When an operator combines two expressions of different data types, the rules for data type precedence specify that the data type with the lower precedence is converted to the data type with the higher precedence. If the conversion is not a supported implicit conversion, an error is returned. When both operand expressions have the same data type, the result of the operation has that data type
So in your case ,CASE is an expression and when two or more data types are combined in this expression,it returns data type of highest precedence..in your case it is INT..So change your query to below
SELECT CASE WHEN ISNUMERIC('1a') =1 THEN cast(1 as varchar) ELSE 'A' END
SELECT CASE WHEN ISNUMERIC('1a') =1 THEN 1 ELSE 2 END
SELECT CASE WHEN ISNUMERIC('1A') = 1 THEN '1' ELSE 'A' END
OR
SELECT CASE WHEN ISNUMERIC('1A') =1 THEN '1' ELSE SUBSTRING('1A',2,2) END

Update table by case ORA-01722 Invalid Number

I am trying to update a table's column based on the value of of two other columns.
For some reason, I am getting ORA-01722: invalid number
UPDATE TableT SET
Col = (CASE when PER in ('1234','2134','2314','3214') AND TYPE = 4 then '4'
when PER in ('34','104','1004') AND TYPE = 4 then '35'
when PER in ('124','1204','2014') AND TYPE = 4 then '36'
ELSE 'Missing'
END);
The only way this statement can fail with ORA-01722 is if Col is a numeric column. '4', '35' and '36' can be automatically converted to a number. However, if none of the case conditions are met, and you end up evaluating the else branch, it will return the string Missing that cannot be converted to a number.
One way to deal with it could be to use null, which was designed to signify a missing value instead:
UPDATE TableT SET
Col = (CASE when PER in ('1234','2134','2314','3214') AND TYPE = 4 then 4
when PER in ('34','104','1004') AND TYPE = 4 then 35
when PER in ('124','1204','2014') AND TYPE = 4 then 36
ELSE NULL
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
---------------------