When I want to multiply three varchar columns with converting to float, I get
Error Converting data type varchar to float
error message from SQL Server.
I use isnumeric and round function in my script too but I receive error too
I think this is because of dot in your varchar which the sql server can't convert it .
if you need to use varchar variables then you have to cast the value before multiplying it.
use cast or convert functions.
select cast (#your_variable_or_column_here as float)
if the point character is an issue, use a replace :
select cast (replace(#your_variable_or_column_here,'.',',') as float)
Related
SQL Server : i want to convert my varchar datatype to float but i do have some scientific notation values in it(for ex. 3.11955748130297E-02) and i am getting following error : Error converting data type varchar to float.
I have tried it using convert and cast functions, but that's not working.
insert into table1--(data type as float)
select convert(float,column1),-- contains '3.11955748130297E-02' as value and datatype is varchar
convert(float,column2)
from table2
the error was coming due to the 'column break' in one of the columns , i tried it using the below code:
convert(float,REPLACE(column1, CHAR(13), '')) column1
thanks a lot for the responses.
I have a column in varchar format and I would like to convert it to float. The values are like:
188234,62
188235
188235,43
188235,88
So, When I try to convert it I get an error:
Error converting data type varchar to float.
I thought that maybe some fields are not numeric and I checked with isnumeric() function, according to the query I concluded that all the values are numeric. Then I tried different queries to convert the field types:
Query1:
SELECT CarData.dbo.FinalData_OilLevel_new_send.KM
CASE WHEN ISNUMERIC(CarData.dbo.FinalData_OilLevel_new_send.KM) = 1
THEN CAST(CarData.dbo.FinalData_OilLevel_new_send.KM AS FLOAT)
ELSE NULL
END
FROM CarData.dbo.FinalData_OilLevel_new_send
Query2:
SELECT CASE ISNUMERIC(KM) WHEN 1 THEN CONVERT(float, KM)) ELSE null END from CarData.dbo.FinalData_OilLevel_new_send
However I always get the same error. Are there some other ways to convert varchar to float?
You have to use a dot as decimal separator:
CAST(REPLACE(CarData.dbo.FinalData_OilLevel_new_send.KM,',','.') AS FLOAT)
At first you need to Replace
The , present
try like this!
select cast(replace(val,',','') as float) from table
SEE DEMO
I am using Sybase and I am doing a select which returns me a column called "iftype", but its type is int and I need to convert into varchar. When I try to do the select without the convert function I get this error:
Error code 257, SQL state 37000: Implicit conversion from datatype 'VARCHAR' to 'INT' is not allowed. Use the CONVERT function to run this query.
I dont know how to implement the function CONVERT. Can anyone help me, please ?
Use the convert function.
SELECT CONVERT(varchar(10), field_name) FROM table_name
Use the STR function:
SELECT STR(field_name) FROM table_name
Arguments
float_expression
Is an expression of approximate numeric (float) data type with a decimal point.
length
Is the total length. This includes decimal point, sign, digits, and spaces. The default is 10.
decimal
Is the number of places to the right of the decimal point. decimal must be less than or equal to 16. If decimal is more than 16 then the result is truncated to sixteen places to the right of the decimal point.
source: https://msdn.microsoft.com/en-us/library/ms189527.aspx
You can use CAST function:
SELECT CAST(your_column_name AS varchar(10)) FROM your_table_name
Actually you don't need to use STR Or Convert. Just select 'xxx'+LTRIM(ColumnName) does the job.
Possibly, LTRIM uses Convert or STR under the hood.
LTRIM also removes need for providing length. It seems to be working for integer or float without worry of truncation.
SELECT LTRIM(ColumnName) FROM TableName
also, LTRIM is better than STR as
SELECT STR(1234567890.123)
gives 1234567890
whereas
SELECT LTRIM(1234567890.123)
gives 1234567890.123
SELECT Cast(Cast([field_name] AS BIGINT) AS NVARCHAR(255))
FROM table_name
CONVERT(DATA_TYPE , Your_Column) is the syntax for CONVERT method in SQL. From this convert function we can convert the data of the Column which is on the right side of the comma (,) to the data type in the left side of the comma (,) Please see below example.
SELECT CONVERT (VARCHAR(10), ColumnName) FROM TableName
I'm getting an error message when trying to do a SUM with a varchar field, I'm getting the following error even when using Cast?
Error converting data type varchar to numeric
SUM(ISNULL(CAST(balance-current_balance_amount /100 AS float),0)) AS bal_diff
The problem is likely that you have non-numeric data in your balance column. If the data were valid, you wouldn't even need a CAST.
SUM(ISNULL((balance-current_balance_amount)/100 ,0)) AS bal_diff
From CAST and CONVERT:
Implicit conversions are those conversions that occur without
specifying either the CAST or CONVERT function.
VARCHAR to NUMERIC or FLOAT are examples of this.
Since you are dealing with balance, which likely has decimal places and possible "$", a CAST to MONEY should fix your issue.
SUM(ISNULL((CAST(balance AS MONEY)-current_balance_amount)/100 ,0))
If you have other non-numeric data in your balance column that is still causing an issue after your CAST to MONEY, you could do a CASE statement in your SELECT
SUM(ISNULL(((CASE WHEN ISNUMERIC(balance) = 0 THEN NULL ELSE CAST(balance AS MONEY) END)-current_balance_amount)/100 ,0))
Or even better, create a new column of type MONEY and start using that.
Assuming balance is a varchar field, you need to cast it to a numeric (float if thats what you want) datatype before trying to do the calculation:
eg/
SUM(
ISNULL(
(CAST(balance AS FLOAT)-current_balance_amount)/100
,0)
) AS bal_diff
I am trying to execute the following Query
select distinct pincode as Pincode,CAST(Date_val as DATE) as Date,
SUM(cast(megh_38 as int)) as 'Postage Realized in Cash',
SUM(cast(megh_39 as int)) as 'MO Commission',
from dbo.arrow_dtp_upg
group by pincode,Date_Val
but I am getting an error "Conversion failed when converting the nvarchar value '82.25' to data type int."
Am I using a wrong data type?
The string "82.25" represents a float (or a decimal) not an int, so use cast(megh_38 as float) if you need a float.
If you require the integer part only then use floor(cast(megh_38 as float)).
You could try this:
cast(megh_38 as decimal(10,2))
cast(megh_39 as decimal(10,2))
One possible problem is delimiter (dot vs comma).
Questions - why you prefer nvarchar for store float or int values? Maybe decimal or float is better solution?