SQL Unable to Convert Varchar to Numeric - Conversion failed error - sql

I need to convert serial numbers in a database table to show as numeric or int. I have attempted to convert them but it does not seem to like certain values like the following: 1.02253e+007. Getting an error stating:
Msg 245, Level 16, State 1, Line 2
Conversion failed when converting the varchar value '1.02253e+007' to
data type int.
Is there something I am leaving out? Any help much appreciated
SELECT [ID]
,[SalesOrder]
,[JobNumber]
,[StockCode]
,SerialNumber
,CONVERT(NUMERIC(16, 0), CAST(CASE
WHEN ISNUMERIC(SerialNumber) = 1 THEN
SerialNumber
ELSE 0
END AS FLOAT))
Receiving error:
Msg 245, Level 16, State 1, Line 2
Conversion failed when converting the varchar value '1.02253e+007' to data type int.

The case is returning an integer, which is your problem. You are doing lots of intermediate conversions that are not necessary. Just do:
(CASE WHEN ISNUMERIC(SerialNumber) = 1
THEN CONVERT(NUMERIC(16, 0), SerialNumber)
ELSE 0
END)
Or better yet:
COALESCE(TRY_CONVERT(NUMERIC(16, 0), SerialNumber), 0)
If you need to support exponential values (such as 1.02253e+007), then you do need the intermediate conversion to float:
TRY_CONVERT(NUMERIC(16, 0),
COALESCE(TRY_CONVERT(float, SerialNumber), 0)
)

Related

T-SQL :: Error converting data type varchar to numeric when changing 1 to 0.5

I'm writing a complex T-SQL query with CASE that will help me calculate how much time it will take me to migrate databases based on the size of each database:
SELECT
ProductVersion
,CompatibilityLevel
,convert(numeric(10,2), substring([Size], 1, (len([Size]) - 3))) as [Size in MB]
,CASE
when ProductVersion < '11%' THEN 1
when ProductVersion = NULL then 1
ELSE ''
END +
CASE
when CompatibilityLevel < '110' then 1
when CompatibilityLevel = NULL then 1
ELSE ''
END +
CASE
when convert(numeric(10,2), substring([Size], 1, (len([Size]) - 3))) < 100.00 THEN 1 -- Here is the problem
ELSE ''
END AS [Hours of work]
FROM MyDatabaseList
All good, it works when I set 1 hour of work for every database which has less then 100.00 MB.
Also all other values are summed and in the Hours of work column you can see numbers like 0, 1, 3, 2...
This means SQL Server is calculating values. All good.
But well, telling to my manager that I have to work 1 hour for a database that only has 100.00 MB of size is ridiculous.
Let's change the line:
when convert(numeric(10,2), substring([Size], 1, (len([Size]) - 3))) < 100.00 THEN 1
and let's put 0.5 instead of 1:
when convert(numeric(10,2), substring([Size], 1, (len([Size]) - 3))) < 100.00 THEN 0.5
...and bang - I get this error:
Msg 8114, Level 16, State 5, Line 1
Error converting data type varchar to numeric.
Questions:
Why this error now?
Why it was successfully calculating before and now needs a datatype somewhere?
What change do I have to add to my query?
Why it says on Line 1? The error must be on Line 16, right?
The error is telling you the conversion is happening in the statement that starts in line 1.
As for why, it's because '' cannot be converted to a numeric but it can be converted to an int.
SELECT CONVERT(decimal(2,1),'')
--Error converting data type varchar to numeric.
GO
SELECT CONVERT(int,'')
--
GO
You were previously converting it to an int (the literal 1 is an int). Get out of the habit of declaring '' for a number; it is by definition not a number, it is a zero length string. If you want zero, then use 0.

How to convert from nchar to decimal in SQL?

I have 2 tables(source and destination) which are respectively NOR_LABOR and ALL_LABOR_DETAILS. In the source table(NOR_LABOR) there is a column "feet_produced" with the data type "nchar(10)". In the destination table(ALL_LABOR_DETAILS) there's a column "labor_feet_produced" with the data type "decimal(18,4)". I want to convert the "feet_produced" from nchar(10) to decimal(18,4) and paste it in the "ALL_LABOR_DETAILS" table's "labor_feet_produced" column.
I have found a code regarding a simillar issue but did not do the exact as I need to do, following is that code snippet :
Select feet_produced AS feet_produced_s, CASE WHEN Isnumeric(feet_produced) = 1
THEN CONVERT(DECIMAL(18,2),feet_produced)
ELSE 0 END AS feet_produced
INTO [MES_DEV].[dbo].[ALL_LABOR_DETAILS]
from [dbo].[NOR_LABOR]
Thank you!
There are values that will test true for IS_NUMERIC, but will fail to convert to decimal.
Instead, use TRY_CONVERT which will return either the successfully-converted-to-decimal value, or a NULL when it fails. (You can then COALESCE to zero to get your desired result).
Here is a short example set of values, using TRY_CONVERT:
SELECT
TryConvert = COALESCE(TRY_CONVERT(decimal(18,4),TestValues),0)
FROM (
VALUES('10.6'),
('ten'),
('7d2'),
('10000000000'),
('10.00000001')
) AS x(TestValues);
The same set of values using your example code will throw an error:
SELECT
IsNumericCase = CASE
WHEN Isnumeric(TestValues) = 1
THEN CONVERT(DECIMAL(18,2),TestValues)
ELSE 0
END
FROM (
VALUES('10.6'),
('ten'),
('7d2'),
('10000000000'),
('10.00000001')
) AS x(TestValues);
This error is returned because 7d2 is numeric, but cannot be converted to decimal.
Msg 8114, Level 16, State 5, Line 14
Error converting data type varchar to numeric.
Im not sure what the issue is with the code that dose not work for you.
But here is how I would do it with a small change in the statement that you just post it.
insert into[MES_DEV].[dbo].[ALL_LABOR_DETAILS](labor_feet_produced)
select CONVERT(DECIMAL(18, 4), feet_produced) AS feet_produced_s from[dbo].[NOR_LABOR]

SQL (Server) error converting varchar to float. Text + amount

This code works and outputs nicely
Code:
select (sum(SchemeAccount.AdoptionAmount)- convert(varchar,Scheme.Acquisition,103)) [£ Output]
Output:
I now want the amount to be preceded by the word 'Variance'
So I try:
select 'variance'+ (sum(SchemeAccount.AdoptionAmount)- convert(varchar,Scheme.Acquisition,103)) [£ Output]
But then get:
Msg 8114, Level 16, State 5, Line 1
Error converting data type varchar to float.
What I want is 'Variance £73,102,500' or at least 'Variance 73102500'
I have tried a lot of CAST and convert(varchar,col,103) functions and have either not been successful or got 'Variance 73e105' or something like that.
If more info is needed on my current data types, I can try to acertain.
Use concat(). I assume you intend:
select concat('variance',
convert(decimal(20, 4), sum(SchemeAccount.AdoptionAmount)),
'-',
convert(varchar(255), Scheme.Acquisition, 103)
) as [£ Output]

Error converting data type varchar to numeric value

I have a problem on executing this query.
select
CASE when (c.Rate=0.00) then a.Item else cast('N/A' AS DECIMAL(5,2)) end
from item a join rate_m c on a.item=c.item where a.item='0781718171'
When I execute this query I got an error as,
Msg 8114, Level 16, State 5, Line 4
Error converting data type varchar to numeric.
and I tried to cast them as above but not successful. Please help me on this and the column c.rate is created as decimal(5,2).
I need output as N/A if c.rate is > 0.00
You cant CAST VARCHAR value to DECIMAL, but you can CAST DECIMAL value to VARCHAR.
Try this:
SELECT
CASE WHEN c.Rate>0.00 THEN 'N/A' ELSE CAST(a.Item AS VARCHAR) END
FROM item a
JOIN rate_m c ON a.item=c.item
WHERE a.item='0781718171'

Error converting data type varchar to numeric -joining tables

I am not able to join two columns with different data types.
main_ref_id is char and person_id are numeric
Query:
select
main_ref_id
from
not
where
main_ref_type = 'P'
and convert(numeric(9, 0), main_ref_id) in (select cast(person_id as numeric)
from person_wo_memberships)
Error
Msg 8114, Level 16, State 5, Line 1
Error converting data type varchar to numeric.
Any help???
You have stated that main_ref_id is char, but you are trying to convert that into a numeric value. The error is self-explanatory.
Try converting person_id, which is numeric to char
select main_ref_id
from [not]
where main_ref_type='P' and
main_ref_id in
(select cast(person_id as varchar)
from person_wo_memberships )
Please define the length of varchar in the cast statement, to match the length of main_ref_id