convert varchar($22.45) to decimal value(22.45) - sql

I have column eng_cost varchar(50), values($22.25), ($10.00), () this is not null value its blank value.
When I exporting this data to table2 column eng_cost decimal(15,5) its giving me error.
Msg 8114, Level 16, State 5, Line 1
Error converting data type varchar to float.
Output:
eng_cost
22.25
10.00
0.00

One method is to convert to money:
select convert(money, '$22.25')
It handles the currency symbol. This can then be converted to another number type.
Or, replace the $:
select convert(float, replace('$22.25', '$', ''))
This is less general if you have different currency symbols.

Related

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]

Update Accumulated Value with CTE error because of decimal datatype (SQL)

I am trying to get the accumulated value of the column MontanteRecuperacao into montanterecuperacao_acumulada, by date and contract.
But the decimal datatype is becoming a problem and I don't know why, this works when the variables are of INT type.
This is the error message
Msg 4187, Level 16, State 1, Procedure spCalculaRecuperacao, Line 123
Data type decimal of receiving variable cannot store all values of the data type decimal of column 'montanterecuperacao_acumulada' without data loss.
Code:
DECLARE #MontanteRecuperacao_running DECIMAL,
#montanterecuperacao_acumulada_running DECIMAL,
#Contrato VARCHAR(10) = '' ;
WITH CTE AS
(
SELECT TOP 100 PERCENT
MontanteRecuperacao, montanterecuperacao_acumulada, ContratoId
FROM
MovimentoRecuperacao
ORDER BY
[ContratoId], [DtReferencia] DESC
)
UPDATE CTE
SET #montanterecuperacao_acumulada_running = montanterecuperacao_acumulada =
CASE
WHEN #Contrato <> ContratoId
THEN montanterecuperacao_acumulada
ELSE #montanterecuperacao_acumulada_running - #MontanteRecuperacao_running
END,
#MontanteRecuperacao_running = MontanteRecuperacao,
#Contrato = ContratoId
You've declared your DECIMAL parameters without specifying precision or scale:
#MontanteRecuperacao_running DECIMAL , #montanterecuperacao_acumulada_running DECIMAL
The default values are DECIMAL(18,0), which is essentially an integer. In order to get the behavior you desire, you'll need to assess your data for proper values to assign.

Splitting field and adding decimal point to create numeric value SQL

I have a field in SQL Server 2014 that I am working with that looks like this:
**RawField**
20060202
20060323
I want to add a split the field and add a decimal point and create a numerical field. This is what I would like to see:
**RawField**
200602.02
200603.23
So I need to split the field, add the decimal point, and convert to a numerical value. I tried some code but was getting an error. Please see my code below:
select top 1000 cast(SUBSTRING(cast(RawField as varchar(6)),1,6) + cast('.' as varchar(1)) + SUBSTRING(cast(RawField as varchar(2)),6,2) as int)
from Table
I get an error of:
Msg 245, Level 16, State 1, Line 11
Conversion failed when converting the varchar value '200602.' to data type int.
Is this a good approach?
you want to convert the string to numeric with 2 decimal places ?
select convert(decimal(10,2), RawField) / 100.0
I guest your RawField contains other alphanumeric after that and you only posted the first 8 characters ?
this should work. Just take the first 8 characters and convert. Simple and direct
select convert(decimal(10,2), left(RawField, 8)) / 100.0
Please try this.
select top 1000 cast(SUBSTRING(cast(RawField as varchar(6)),1,6) + cast('.' as varchar(1)) + SUBSTRING(cast(RawField as varchar(2)),6,2) as numeric(8,2))
from Table
You are trying to cast string with decimal number to int.
Use float/numeric/decimal when casting
select cast(SUBSTRING(RawField ,1,6) + cast('.' as varchar(1)) + SUBSTRING(RawField ,7,2) as numeric(16,2))

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