SQL Server : conversion error numeric to varchar case - sql

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',

Related

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

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.

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;

Data conversion error when using floor function in sql

please help me on this.
i am trying to check if .two decimal value not in a column using this.
FLOOR(LOG10(REVERSE(ABS(M.Description_12_Value)+1)))+1 <> 2
but i am getting error
Conversion failed when converting the varchar value '8.5' to data
type int.
Thanks
One of the columns you are comparing, either Description_2_Value/Description_3_Value/Description_10_Value is returning that 8.5 value and hence the query is throwing the error while comparing '8.5' > something. You need to cast the value RHS value in your condition to a varchar to fix it. See working example below:
if ('2.5' > '2')
begin
print 'success'
end

Conversion of variable characters

I use this statement within a query to get a blank result with a WHEN/THEN statement but keep getting the following error message:
" The conversion of a varchar data type to a datetime data type resulted in an out-of-range value." Below is the statement:
CASE
WHEN (A.APRDAT+'-'+A.APRBAT)= 01/01/1800
THEN ' '
ELSE
Convert(NVARchar(20),A.APRDAT)+'-'+Convert(NVARchar(20),A.APRBAT)
END AS AUTH_ID,
Could I get some assistance in getting this to work as design..thanks
Date literals need to be in single quotes, and you'll need to CONVERT the string result:
WHEN CONVERT(DATETIME,A.APRDAT+'-'+A.APRBAT) = '01/01/1800'
THEN ' '
ELSE
Convert(NVARchar(20),A.APRDAT)+'-'+Convert(NVARchar(20),A.APRBAT)

Why am I getting `Conversion failed when converting the varchar value 'This is full' to data type int.`?

This code has been working for the past one year but suddenly, we started getting:
Conversion failed when converting the varchar value 'This is full' to data type int
Code:
CASE
WHEN L.Seating_Capacity - COALESCE(TS.TakenSeats,0) = 0 THEN 'This is full'
WHEN l.location = 'MLK High School' AND d.trainingDates = '5/14/2014' THEN 70 - COALESCE(TS.TakenSeats,0)
ELSE CAST(L.Seating_Capacity - COALESCE(TS.TakenSeats,0) AS VARCHAR)
END AS 'AvailableSeats'
Any ideas what went wrong?
Because CASE is an expression - it computes a single value, of a single type. So all of the possible THENs (and the ELSE) must all produce values that can be converted to a single type. You can't have one THEN produce a varchar and a different one produce an int.
And since int has a higher precedence than varchar, that's the single data type that SQL Server tries to convert everything to.
CASE WHEN L.Seating_Capacity - COALESCE(TS.TakenSeats,0) = 0
THEN 'This is full'
WHEN l.location='MLK High School' AND d.trainingDates = '5/14/2014'
THEN CONVERT(varchar(10),70 - COALESCE(TS.TakenSeats,0))
ELSE CONVERT(varchar(10),L.Seating_Capacity - COALESCE(TS.TakenSeats,0))
END AS 'AvailableSeats'