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

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]

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]

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

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.

SQL Unable to Convert Varchar to Numeric - Conversion failed error

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)
)

Apply IsNull function on a date column (SQL Server)

I am trying to apply the IsNull function to a column of a datatype date.
What am trying to do is, to replace the word NULL with an empty string (actual null value) when retrieving the data
The code am using is;
Select
ISNULL("UPDATE_DT", '')
from
MyTable
I have also tried
Select
ISNULL("UPDATE_DT", 0)
from
MyTable
The error am getting is
Msg 103010, Level 16, State 1, Line 1
Parse error at line: 4, column: 1: Incorrect syntax near 'from'.
If you want to see the column as an empty string rather than "NULL", you need to convert the output to a string. To control the format, use convert():
select coalesce(convert(varchar(10), update_dt, 121), '')
. . .
Then use coalesce() (or if you must, isnull()) to replace the NULL value with another string.

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