T-SQL converting string into float - sql-server

In a stored procedure on my SQL Server, I am trying to convert values from a varchar column into a float format.
The values into the varchar column are numbers with a sign at the beginning and a '.' before decimals.
Examples: '+0000000000000044.09' or '-0000000000114995.61'
If I try this: convert(float,mystring), it doesn't work.
I have:
Error converting data type varchar to float
Is this kind of conversion possible?
Or is there another way to convert a string with a sign and a '.' into a float?

As your examples both work, I'd guess there's another value somewhere in your table that's causing the problem. In recent versions of SQL Server (2012 onwards), the TRY_CONVERT function can be useful for tracking down this kind of issue.
TRY_CONVERT will not throw an exception on a conversion failure, but instead return a NULL value, so you can figure out which values are causing the problem like this:
SELECT * FROM your_table WHERE TRY_CONVERT(FLOAT, your_column_name) IS NULL
If any rows are returned, those are the rows with problem values that can't be converted to FLOAT.

You can try using CAST(mystring as float) or TRY_CONVERT(float,mystring).
Thuough convert(float,mystring) also should work fine. I would suggest checking your data.

Related

SQL CAST with ISNULL results in conversionfailure

I've got a nullable column of numbers, which is unfortunately declared as a varchar in the database, and so I want to convert the NULL to 0. Doing this accomplishes that.
select ISNULL(col, '0') from table;
So now there are no null possibilities, and then I want to convert the column to an actual int value, so I tried to wrap that.
select CAST(ISNULL(col, '0') AS INT) from table
When that runs I get a conversion error, as shown below. I don't understand why, or how to get around this:
Conversion failed when converting the varchar value 'NULL' to data type int.
Try like this: Take your ISNULL statement outside
SELECT ISNULL(TRY_CAST(col AS INT),0) AS [col]
FROM SAMPLE_TAB;
If this does not help, then please comment your SQL version.

Error converting data type nvarchar to float in SQL Server 2008

select sum(cast(mmax as float)
from table
mmax is of datatype nvarchar and the value is
string,int,decimal, value
I trying to sum of like value 17.50,35.00.
I am avoiding string value in where clause
But not solved this problem
Error is thrown
String/Varchar values with commas such as "10,000" pass the IsNumeric() test but do not cast/convert into numeric types without raising an error.
You can replace the commas and perform the cast and sum operation:
select sum(cast(replace(mmax,',','') as float))
from tbl
where isnumeric(maxx)>0
One of the values cannot be converted to a float. You may have a million values that can convert if one (has a letter O instead of a 0 for example) you will get that message.

How do I convert a varchar containing a number in scientific notation to a numeric in SQL Server?

I made some calculations using function and get the following value. I want to divide this value with 1000.
1.83673e+006
I want to convert the value into numeric.
When I try to convert it to numeric it throws
Error converting data type nvarchar to numeric.
Is it possible to convert this value to numeric? Please help me to get this.
Thanks in advance
try it
SELECT CONVERT(numeric(16,0), CAST(1.83673e+006 AS FLOAT))
In SQL Server, scientific notation (...+e...) is only used for floating-point data types, not for decimal/numeric data types.
Thus, SQL Server recognizes this format only when converting to a floating-point data type (such as float or real), not when converting directly to numeric.
Obviously, nothing prevents you from converting the value to float first and then to numeric:
SELECT CONVERT(numeric(19,4), CONVERT(float, '1.83673e+006'))
yields 1836730.0000.
Just try this
Declare #data varchar(100)='1.83673e+006'
SELECT #data=CAST(CAST( #data AS float) AS NUMERIC)/1000
SELECT #data Result
Result
1836.730000

data conversion from varchar to decimal

I have a column with varchar data type. I need to convert it into decimal data type in sql server 2014?
For example, varchar value .0462500 into decimal 4.62500
I have seen some similar questions in so, but it didn't work for me.
Thank you for your time and help!
If you know that your VARCHAR variables will require roughly the same conversion rate, you could simply use the CAST() function to convert them and then perform the necessary multiplication :
SELECT CAST(YourValue AS DECIMAL(7,5)) * 100
Likewise the CONVERT() method will essentially accomplish the same thing via a different syntax :
SELECT CONVERT(DECIMAL(7,5),YourValue) * 100
You can use Cast or Convert functions.
Convert:
select convert(decimal(10,5),.0462500)*100
Cast:
select cast(.0462500 as decimal(10,5))*100
Note: If there is malformed data(data which can not be converted), you may need to use Try_Convert or Try_Parse functions instead.
Try_Convert:
select try_convert(decimal(10,5),.0462500)*100
Try_Parse:
select try_parse('.0462500' as decimal(10,5))*100

Convert long decimal or float to varchar in SQL Server

One of the table I'm trying to query has a field with type decimal(38,19). I need to convert it to varchar in order for my perl DBI module to handle. How should I write the conversion in SQL to make it work? Specifically, if I run this in SQL Server Management Studio:
select convert(varchar, 19040220000.0000000000000000000)
I get:
Msg 8115, Level 16, State 5, Line 1
Arithmetic overflow error converting numeric to data type varchar.
I tried to round the number first:
select convert(varchar, round(19040220000.0000000000000000000, 0))
but that doesn't seem to work either (same error message). In fact round() doesn't seem to have an effect on that number for some reason. What should I do? Thx.
If you don't specify a length for your varchar, it defaults to 30 characters in the case of a CONVERT operation.
That's not long enough to hold your 38-digit decimal. So give your varchar an appropriate length in the CONVERT statement!!
Try this:
select convert(varchar(40), 19040220000.0000000000000000000)
You need to use a varchar with a set length larger than the precision you want, i.e.
select convert(varchar(64), 19040220000.0000000000000000000)

Resources