Convert long decimal or float to varchar in SQL Server - 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)

Related

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.

T-SQL converting string into float

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.

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

Conversion failed when converting the varchar value '12/31/'

I have the following code:
declare #StartDate date
declare #EndDate date
set #StartDate='09/01/2016'
set #EndDate='11/30/2016'
SELECT
1 AS Segment,
'C' AS Subsegment,
100 AS Ent,
'KPAR' AS LOB,
'ATLB' AS Cov,
ClaimNumber AS [Claim#],
'12/31/'+AccidentYear AS AYDate,
convert(date, cast(AccountingPeriodDate as date),101) AS EvalDate
However I get an error
Conversion failed when converting the varchar value '12/31/' to data type int.
I haven't gotten this error before, and was wondering it it's due to a datatype change.
First, you should use ISO standard date formats, such as YYYYMMDD and YYYY-MM-DD.
Then, be explicit about the conversion and use try_convert(). So, I would write this as:
select try_convert(date, concat(accident_year, '1231'))
concat() will automatically convert the number to a date.
Or, alternatively, use datefromparts():
select datefromparts(accident_year, 12, 31)
This is probably the simplest solution.
Probably need to cast accidentyear as varchar
DECLARE #accidentyear AS INT
SET #accidentyear = 2016
SELECT '12/31/' + CAST(#accidentyear AS VARCHAR(4))
try this:
SELECT 1 AS Segment
,'C' AS Subsegment
,100 AS Ent
,'KPAR' AS LOB
,'ATLB' AS Cov
,ClaimNumber AS [Claim#]
,'12/31/'+ CAST(AccidentYear as nvarchar(4)) AS AYDate
,convert(date, cast(AccountingPeriodDate as date),101) AS EvalDate
The advice you got from other users to cast/convert accidentYear to varchar is correct. The reason this is necessary is because the data types are not the same. '12/31' is clearly a string. We also know that AccidentYear is an int because the error message says it's an int.
Since the data types are different, SQL Server attempts an implicit data type conversion. When SQL Server does this, the string is converted to an integer and then math is performed on it.
Since implicit conversion is not possible, you need to use explicit conversion instead. By using the cast or convert function, you are doing an explicit conversion.
The rules for implicit data type conversions can be found here: https://msdn.microsoft.com/en-us/library/ms190309.aspx?f=255&MSPPError=-2147217396

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

Resources