When using cast - error: invalid input syntax for type numeric: "" (postgreSQL) - sql

Duration column in the table is given with 'varchar' data type. It contains decimal values. So I am trying to cast varchar to float/numeric/decimal/double/double precision. But none of those works. why is it not working?
select runner_id,
sum(case when cast(duration as decimal) <> '' then 1
else 0 end) as delivered, count(order_id) as total_orders
from t_runner_orders
group by runner_id

The reason it's not working is because your duration column contains values which cannot be cast to a numeric type. The specific value throwing the error is an empty string. Also, you shouldn't be comparing a numeric type to an empty string.
Also, if you're comparing a varchar column to a character value in your CASE statement, why are you trying to cast it to a numeric type at all?
For what you're doing here, I would just write it as CASE WHEN duration <> '' THEN 1 ELSE 0 END
And if you do need to cast it to a numeric type at some point, the way to do that would be something like CASE WHEN duration = '' THEN NULL ELSE cast(duration AS DECIMAL) END (asuming that empty strings are the only values in your column which cannot be cast to decimal)

The problem is you are doing the CAST before the <> ''. The cast fails as there are empty strings in the field. You have several choices:
Use NULL instead of '' in field.
Do duration <> ''
Last and probably the best long term solution change the column type to numeric.

You can translate '' to null using NULLIF in the cast.
cast(nullif(duration,'') as decimal) is not null
However this will not solve you basic problem which is "varchar' data type. It contains decimal values" NO it does not it contains a string which you hope are decimal values, but nothing prohibits putting 'zero.zero' into it - distinctly not a decimal value. I will go #AdrianKlaver one step further.
3. The only long term solution change the column type to numeric.

Related

how to show empty value for a int datatype in SQL?

how to show empty value for a int datatype in SQL?
I have a case statement on an int datatype column to show empty for the values less than or equal to 0.
case when [TotalValue] <= 0 Then ''
when [TotalValue] > 0 Then [TotalValue]
End as [TotalValue]
Right now, case statement is returning 0 for any values less than or equal to 0. I expect to have them as Empty. Having 0 instead of negative value is not a correct result.
How to convert the record to show only empty?
The problem of your code is that Then '' is automatically converted to int value, which happens to be 0 for empty strings (try select CAST('' as int) to check).The data type for ambiguously defined column (like yours) is determined from the data type precedence rules.
Unambiguously defining the data type of the column would resolve the issue.
I recommend trying to return NULL from the database, like this:
case when [TotalValue] <= 0 Then NULL
when [TotalValue] > 0 Then [TotalValue]
End as [TotalValue]
Most likely, your report engine will convert NULL to something like an empty string. In addition, you may be getting some benefits of ability to manipulate numeric values, if your report engine supports those (e.g. calculate average over selection).
Alternatively, try casting the values to string in SQL:
case when [TotalValue] <= 0 Then ''
when [TotalValue] > 0 Then CAST([TotalValue] as varchar)
End as [TotalValue]
I think the simplest construct is:
(case when TotalValue > 0 Then TotalValue
end) as TotalValue
You can always CAST the number to a VARCHAR (string) and then set it to an empty string when NULL:
ISNULL(CAST(TotalValue as varchar(10)),'') as TotalValue
Empty string is a concept that doesn't make sense for integer datatype.
Generally you should return the results to the application as integer datatype and use NULL for this and have your application display null as an empty string if desired.
If you do need to do this in SQL you are now dealing with strings rather than integers.
One way of converting to string and performing your desired formatting is with the FORMAT function.
SELECT FORMAT(TotalValue, '0;"";""')
FROM
(VALUES (1),
(0),
(-123),
(123456))T(TotalValue)
Returns
1
123456

CAST and CASE in SQL SELECT statement

I'm trying to return a string when certain conditions are true, but the I'm running into a data type issue... LI.num_seats_pur and LI.num_seats_ret are both smallint data types...
Here's where I'm stuck:
SELECT
CASE
WHEN (LI.num_seats_ret = LI.num_seats_pur)
THEN 'RET'
ELSE (LI.num_seats_pur - LI.num_seats_ret)
END as 'Seats'
FROM T_LINEITEM LI;
I understand that 'RET' is obviously not a smallint, but every combination of CAST I use here is still causing an error. Any ideas?
When using a CASE expression, if the return values have different data types, they will be converted to the one with the higher data type precedence. And since SMALLINT has a higher precedence than VARCHAR, the return value of the ELSE part, 'RET' gets converted to SMALLINT. This will then proceed to a conversion error:
Conversion failed when converting the varchar value 'RET' to data type smallint.
In order to achieve the desired result, you need to CAST the ELSE part to VARCHAR:
SELECT
CASE
WHEN (LI.num_seats_ret = LI.num_seats_pur)
THEN 'RET'
ELSE
CAST((LI.num_seats_pur - LI.num_seats_ret) AS VARCHAR(10))
END AS 'Seats'
FROM T_LINEITEM LI;

SQL Server : conversion error numeric to varchar case

I have this query:
CASE ClaimsFees.TBA
WHEN 0 THEN CAST(IndemnityReserve AS NUMERIC(9,2))
ELSE 'TBA'
END AS 'Reserve Indemnity'
but I always get this error:
Error converting data type varchar to numeric
I have tried to convert TBA as numeric but I can't do this, I also can't convert all the results to varchar because when I transfer to Excel file the number 1.325,27 becomes 132.527,00 with the ##,##0.00 format.
Is there a method in SQL Server that I can use to solve this?
The column can only have one type, and as 'TBA' can only be a string, you'll need to make the numeric a string too.
CASE ClaimsFees.TBA WHEN 0
then cast(cast(IndemnityReserve as NUMERIC(9,2)) as varchar(12))
else 'TBA'
end as 'Reserve Indemnity',

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

Conversion failed when converting the varchar value 'Yes' to data type bit

I used a case to change boolean column values to a text string that actually means something. Firstly, when doing a WHERE statement on these columns do I use the boolean values or the updated text strings?
I used:
WHERE TrainingProgram.blnInCatalogue = 'Yes'
AND TrainingProgram.blnActive = 'Inactive'
But I get the error:
Conversion failed when converting the varchar value 'Yes' to data type bit.
Am I doing something wrong?
For true, try using:
WHERE TrainingProgram.blnInCatalogue <> 0
For false, try using:
WHERE TrainingProgram.blnInCatalogue = 0
The blnInCatalogue column is a bit field, and it doesn't understand what the string 'Yes' means. It only knows 0 or 1.
Though you can use a case to display bit columns as readable strings, their internal value is still bit. They can only be compared to 0 or 1.