CASE statement where conditional includes an IN statement redshift - sql

CASE
WHEN code IN ('FJS354', 'JDF334')
THEN 'Lower_form'
ELSE 0
END AS format
This returns an error in Redshift
invalid input syntax for integer: "Lower_form"
I know if I change 'Lower_form' to an integer it will work however I want this column to be a string. Is there a way to do this?

I want this column to be a string.
All branches of a case expression must return the same datatype. You are giving two literal values whose datatype is not the same (string vs integer): the database makes the decision to turn them both to integers - which is not what you want.
Rremove the ambiguity by being explicit about the datatype you want to return. That is, make this literal 0 a string:
CASE WHEN code in ('FJS354','JDF334')
THEN 'Lower_form'
ELSE '0'
END as format

Related

How to identify long value using sql

I need to check whether the values received in a file upload column has exponential or long values.
For example, if value is 5.02E+13 instead of numeric value - 50100434157080 then need to restrict it with a message saying format is incorrect.
For this I pass the upload content from frontend to backend in a temporary table and then get it checked if passed value has exponential value or numeric value.
Tried using T-SQL function isnumeric() but it didn't give me expected result. Any other function available?
Since you mention using isnumeric I assume you are using SQL Server, in which case you can try try_cast, for example
select case when Try_Cast(Column as bigint) is null then 'not integer' else 'integer' end
from table
You could also use like
select case when Column like '%e%' then 'exponent' else 'number' end
from table

How to check if column is string

How to check in hive whether the column is holding string character. My column is string datatype and has values like below
my_col string
000001234
0001111
One way to check, whether string character is present is following:
SELECT
my_col,
CASE WHEN HEX(LOWER(my_col)) <> HEX(UPPER(my_col)) then 'char present' else 'no char' END char_check
FROM my_table;
We convert value to lower case and upper case and cast it to hexadecimal.
HEX('1a') produces 3161
HEX('1A') produces 3141
And that's how we know that a character is present which can be converted to upper or lower case.
It's a quick workaround, otherwise I would create UDF with Java.

SQL CAST String to Decimal

SQL0802 Data conversion or data mapping error
The fields not being CAST are decimals. The fields I am trying to CAST are strings.
I have tried different variations or CAST and CONVERT on this case expression. I'm fairly certain this syntax is correct. I am still getting the error though.
CASE WHEN cpssn=amssn THEN amfnam||amlnam
WHEN cpssn=CAST(maassn as DECIMAL(9)) THEN maafnm||maalnm
WHEN cpssn=CAST(mpssno as DECIMAL(9)) THEN mppfnm||mpplnm
END as Name
One brute force method uses translate():
(CASE WHEN cpssn = amssn THEN amfnam||amlnam
WHEN length(translate(massn, 'a0123456789', 'a')) > 0 THEN NULL
WHEN length(translate(mpssno, 'a0123456789', 'a')) > 0 THEN NULL
WHEN cpssn = CAST(maassn as DECIMAL(9)) THEN maafnm||maalnm
WHEN cpssn = CAST(mpssno as DECIMAL(9)) THEN mppfnm||mpplnm
END) as Name
Note: I am not intimately familiar with translate() in DB2. The above uses an Oracle convention for removing characters, because the third argument cannot be '' in Oracle. It should still work in DB2.
This should work by first guaranteeing that the only characters in the strings are digits. case is processed in sequential order, so the digit checks should be done before the conversion.

Converting char to integer in INSERT using IIF and SIMILAR TO

I am using in insert statement to convert BDE table (source) to a Firebird table (destination) using IB Datapump. So the INSERT statement is fed by source table values via parameters. One of the source field parameters is alphanum (SOURCECHAR10 char(10), holds mostly integers and needs to be converted to integer in the (integer type) destination column NEWINTFLD. If SOURCECHAR10 is not numeric, I want to assign 0 to NEWINTFLD.
I use IIF and SIMILAR to to test whether the string is numeric, and assign 0 if not numeric as follows:
INSERT INTO "DEST_TABLE" (......, "NEWINTFLD",.....)
VALUES(..., IIF( :"SOURCECHAR10" SIMILAR TO '[[:DIGIT:]]*', :"SOURCECHAR10", 0),..)
For every non numeric string however, I still get conversion errors (DSQL error code = -303).
I tested with only constants in the IIF result fields like SOURCECHAR10" SIMILAR TO '[[:DIGIT:]]*', 1, 0) and that works fine so somehow the :SOURCECHAR10 in the true result field of the IIF generates the error.
Any ideas how to get around this?
When your query is executed, the parser will notice that second use of :"SOURCECHAR10" is used in a place where an integer is expected. Therefor it will always convert the contents of :SOURCECHAR10 into an integer for that position, even though it is not used if the string is non-integer.
In reality Firebird does not use :"SOURCECHAR10" as parameters, but your connection library will convert it to two separate parameter placeholders ? and the type of the second placeholder will be INTEGER. So the conversion happens before the actual query is executed.
The solution is probably (I didn't test it, might contain syntax errors) to use something like (NOTE: see second example for correct solution):
CASE
WHEN :"SOURCECHAR10" SIMILAR TO '[[:DIGIT:]]*'
THEN CAST(:"SOURCECHAR10" AS INTEGER)
ELSE 0
END
This doesn't work as this is interpreted as a cast of the parameter itself, see CAST() item 'Casting input fields'
If this does not work, you could also attempt to add an explicit cast to VARCHAR around :"SOURCECHAR10" to make sure the parameter is correctly identified as being VARCHAR:
CASE
WHEN :"SOURCECHAR10" SIMILAR TO '[[:DIGIT:]]*'
THEN CAST(CAST(:"SOURCECHAR10" AS VARCHAR(10) AS INTEGER)
ELSE 0
END
Here the inner cast is applied to the parameter itself, the outer cast is applied when the CASE expression is evaluated to true

Conditionally branching in SQL based on the type of a variable

I'm selecting a value out of a table that can either be an integer or a nvarchar. It's stored as nvarchar. I want to conditionally call a function that will convert this value if it is an integer (that is, if it can be converted into an integer), otherwise I want to select the nvarchar with no conversion.
This is hitting a SQL Server 2005 database.
select case
when T.Value (is integer) then SomeConversionFunction(T.Value)
else T.Value
end as SomeAlias
from SomeTable T
Note that it is the "(is integer)" part that I'm having trouble with. Thanks in advance.
UPDATE
Check the comment on Ian's answer. It explains the why and the what a little better. Thanks to everyone for their thoughts.
select case
when ISNUMERIC(T.Value) then T.Value
else SomeConversionFunction(T.Value)
end as SomeAlias
Also, have you considered using the sql_variant data type?
The result set can only have one type associated with it for each column, you will get an error if the first row converts to an integer and there are strings that follow:
Msg 245, Level 16, State 1, Line 1
Conversion failed when converting the nvarchar value 'word' to data type int.
try this to see:
create table testing
(
strangevalue nvarchar(10)
)
insert into testing values (1)
insert into testing values ('word')
select * from testing
select
case
when ISNUMERIC(strangevalue)=1 THEN CONVERT(int,strangevalue)
ELSE strangevalue
END
FROM testing
best bet is to return two columns:
select
case
when ISNUMERIC(strangevalue)=1 THEN CONVERT(int,strangevalue)
ELSE NULL
END AS StrangvalueINT
,case
when ISNUMERIC(strangevalue)=1 THEN NULL
ELSE strangevalue
END AS StrangvalueString
FROM testing
or your application can test for numeric and do your special processing.
You can't have a column that is sometimes an integer and sometimes a string. Return the string and check it using int.TryParse() in the client code.
ISNUMERIC. However, this accepts +, - and decimals so more work is needed.
However, you can't have the columns as both datatypes in one go: you'll need 2 columns.
I'd suggest that you deal with this in your client or use an ISNUMERIC replacement
IsNumeric will get you part of the way there. You can then add some further code to check whether it is an integer
for example:
select top 10
case
when isnumeric(mycolumn) = 1 then
case
when convert(int, mycolumn) = mycolumn then
'integer'
else
'number but not an integer'
end
else
'not a number'
end
from mytable
To clarify some other answers, your SQL statement can't return different data types in one column (it looks like the other answers are saying you can't store different data types in one column - yours are all strign represenations).
Therefore, if you use ISNUMERIC or another function, the value will be cast as a string in the table that is returned anyway if there are other strigns being selected.
If you are selecting only one value then it could return a string or a number, however your front end code will need to be able to return the different data types.
Just to add to some of the other comments about not being able to return different data types in the same column... Database columns should know what datatype they are holding. If they don't then that should be a BIG red flag that you have a design problem somewhere, which almost guarantees future headaches (like this one).