Updating an array and converting from varchar to integer - arrays

I'm updating an array by adding a varchar to an int4 array. The 'varchar' field is all numbers so I tried casting it to ::integer but it isn't working
update dwh.attr_lookup set kli_tree = array[target_909_kli::integer] || kli_tree
is giving me this error
ERROR: ERROR: invalid input syntax for integer: ""
Query = update
dwh.attr_lookup set kli_tree = array[target_909_kli::integer]
|| kli_tree
What is the proper way to do this?

You're trying to cast an empty string to an integer and that doesn't work:
=> select ''::int;
ERROR: invalid input syntax for integer: ""
LINE 1: select ''::int;
^
You'll have to decide what you want to do with empty strings. If you want to convert them to zeros, then something like this should work:
array[case when target_909_kli = '' then 0 else target_909_kli::integer end]

Obviously your varchar field is not all numbers. There would not be any double quotes in this case - as the error message informs us.
Try giving a full example if the error has not clear by now. Including table definition and sample values.

Related

Snowflake Numeric value '' is not recognized

Please help me with error 'Numeric value '' is not recognized'.
In Snowflake i am having a variable day_minus which holds a number, using this i want to return date value of current_date minus the value given in the variable.
If there is Null or empty passed in the variable i want to get current date minus 1.
For this i have written a code like below. But it throws me an error Numeric value '' is not recognized
But it throws an
set day_minus=7;
SELECT DATEADD(DAY, concat('-',nvl(nullif(try_to_number($day_minus) ,''),1)),current_date() );
Can you please correct me where i am doing mistake
you can use case statement
SELECT DATEADD(DAY, concat('-',nvl(case when $Units ='' then 1 else $Units end ,1)),current_date() );
If you use concat then the result is always going to be a string. If you want to set the "default" value to -1 then just put -1 in your code.
The try_to_* functions take a string as their input so you'd need to make your variable a string to make this work e.g.
set day_minus='7';
You don't need nullif at all. So the final version would be something like:
set day_minus='7';
DATEADD(DAY, nvl(try_to_number($day_minus),-1),current_date() );

Defined expression in SSRS report is displaying #ERROR

I have defined an expression in one of my cells from my dataset1. My Design window and I have to repeat for each month's cells but I'm getting an #ERROR when I click on the PREVIEW tab in SSRS.
My thought is if the ActivityMonth value = 1 and the Type value = "PIF" then display the value in the Data column.
=IIF(Fields!Activity_Month.Value = 1 AND Fields!Type.Value = "PIF", Fields!Data.Value, 0)
I got this WARNING from SSRS:
[rsRuntimeErrorInExpression] The Value expression for the textrun ‘Textbox1471.Paragraphs[0].TextRuns[0]’ contains an error: Input string was not in a correct format.
But it ran successfully.
From the comments and the edit history, it looks like you have used & mark which is used to concatenate strings instead of AND keyword. After editing the expression - for me - the following expression looks great:
=IIF(Fields!Activity_Month.Value = 1 AND Fields!Type.Value = "PIF", Fields!Data.Value, 0)
But i have two remarks:
It may cause an error due to the different data types returned by the expression (0 is integer, Data.Value has another data type:
If Fields!Data.Value is of type string then use the following expression:
=IIF(Fields!Activity_Month.Value = 1 AND Fields!Type.Value = "PIF", Fields!Data.Value, "0")
Another thing to mention is that if value contains null it may throw an exception, so you have to check if field contains null:
SSRS expression replace NULL with another field value
isnull in SSRS expressions

What are all values ISNUMERIC function finds as TRUE in sql server?

Below part of my query was failing initially:
WHEN ISNUMERIC(npx.nvcAnswer) = 1
THEN CASE
WHEN ABS(CONVERT(DECIMAL(38,2),npx.nvcAnswer)) < 1
THEN CONVERT(VARCHAR,CONVERT(DECIMAL(38,2),npx.nvcAnswer))
ELSE npx.nvcAnswer
END
Below was the error we were getting:
com.microsoft.sqlserver.jdbc.SQLServerException: Error converting data type nvarchar to numeric.
rootCause=SQLException #1: errorCode: 8114 sqlState: S0005 message: Error converting data type nvarchar to numeric.
I realized that it considers '.' & '-' as NUMERIC too.
So I added "NOT npx.nvcAnswer in( '.' , '-')" to it and it worked.
WHEN ISNUMERIC(npx.nvcAnswer) = 1 AND NOT npx.nvcAnswer in( '.' , '-')
THEN CASE
WHEN ABS(CONVERT(DECIMAL(38,2),npx.nvcAnswer)) < 1
THEN CONVERT(VARCHAR,CONVERT(DECIMAL(38,2),npx.nvcAnswer))
ELSE npx.nvcAnswer
END
After some day it started failing again with below error.
com.microsoft.sqlserver.jdbc.SQLServerException: Error converting data type nvarchar to numeric.
rootCause=SQLException #1: errorCode: 8114 sqlState: S0005 message: Error converting data type nvarchar to numeric.
Then I changed below block and it worked: (But I failed to find what values were causing this issue)
OLD:
WHEN ABS(CONVERT(DECIMAL(38,2),npx.nvcAnswer)) < 1
THEN CONVERT(VARCHAR,CONVERT(DECIMAL(38,2),npx.nvcAnswer))
NEW:
WHEN ABS(CONVERT(DECIMAL(38,2),PATINDEX('%[^0-9]%', npx.nvcAnswer))) < 1
THEN CONVERT(VARCHAR,CONVERT(DECIMAL(38,2),PATINDEX('%[^0-9]%', npx.nvcAnswer)))
Ask :
What are all values ISNUMERIC function finds as TRUE in sql server?
I will suggest you should your Try_Cast() function instead of using complex logic to check numeric values.
ISNUMERIC returns 1 for some characters that are not numbers, such as plus (+), minus (-), and valid currency symbols such as the dollar sign ($). Please refer below links for further detail -
https://learn.microsoft.com/en-us/sql/t-sql/functions/isnumeric-transact-sql

SQL: How to Store Integer value in CASE function in sql?

I'm working in a Sybase database.I want the 3rd 'WHEN' statement as Integer as output of Original value(Round((sum(value)*100),1)). How can I achieve this. Thanks in advance.
CASE WHEN Round((sum(value)*100),1)<0 THEN 'Neg'
WHEN Round((sum(value)*100),1)=0 THEN 'N/A'
WHEN Round((sum(value)*100),1)>0 THEN Round((sum(value)*100),1)
END AS RCB
Note: here 3rd row indicating error as it is an integer.The error, I'm getting is :
Error:- Data exception - data type conversion is not possible. In CASE expression, incompatible data type at result expression 3, ROUND(COALESCE ((SUM(MSTR.SUM.value)*100),0),1)
Secondly , I tried with Else statement below.
CASE WHEN Round((sum(value)*100),1)<0 THEN 'Neg'
WHEN Round((sum(value)*100),1)=0 THEN 'N/A'
--WHEN Round((sum(value)*100),1)>0 THEN Round((sum(value)*100),1)
ELSE Round((sum(value)*100),1)
END AS RoRC,
But still getting the same error. Please help me out. Thanks.
You need to cast the number as varchar to match the type of other possible results of CASE statement
CASE WHEN Round((sum(value)*100),1)<0 THEN 'Neg'
WHEN Round((sum(value)*100),1)=0 THEN 'N/A'
WHEN Round((sum(value)*100),1)>0 THEN CAST(Round((sum(value)*100),1) as varchar)

Why is my CASE statement requiring the THEN part to be of data type INT?

I am trying to run a query where the below CASE statement is one of the lines. I'm using Report Builder 3.0.
However, I get an error that says :
Conversion failed when converting the varchar value 'Case 1' to data type int; Microsoft SQL Server, Error: 245".
Why is the CASE statement requiring the THEN part to be an INT data type ?
(CASE
WHEN jobs.Uf_Production_Line = 'LN BM6'
THEN 'Case 1'
ELSE
99999
END
) AS line
When using CASE statement, all result expressions must have the same data type. If not, the result will be converted to the data type with a higher precedence. According to BOL
Returns the highest precedence type from the set of types in
result_expressions and the optional else_result_expression.
And since INT has a higher data type precedence than VARCHAR, your query produces an error:
Conversion failed when converting the varchar value 'Case 1' to data
type int; Microsoft SQL Server, Error: 245".
To fix this, you should convert your ELSE part to VARCHAR.
ELSE '99999'
Because your else uses a value with data type int:
ELSE 99999
You use data types (varchar and int) that can't be exchanged automatically.
An option is to use:
ELSE '99999'
You should use '99999' instead of 99999. All values should be the same datatype in CASE, for now in your ELSE part values is of INT datatype.
Full code should be like:
(CASE WHEN jobs.Uf_Production_Line = 'LN BM6' THEN 'Case 1'
ELSE '99999'
END
) AS line
A case statement can only return one data type. So use the number as varchar:
(CASE WHEN jobs.Uf_Production_Line = 'LN BM6'
THEN 'Case 1'
ELSE '99999'
END
) AS line
OR you can return NULL instead of 9999 if you want to represent it as an invalid value for Line:
(CASE WHEN jobs.Uf_Production_Line = 'LN BM6'
THEN 'Case 1'
ELSE NULL
END
) AS line
Because 99999 is an int. Both cases must return the same data type.

Resources