Format Round sum of decimal column in sql - sql-server

Query:
format(round((sum(AllowedAmount) /count(ALLOWEDUNITS) ) ,2),'N2') as avg_allow_per_unit
gives me the right result, but avg_allow_per_unit creates as nvarchar(4000)
I need field 'avg_allow_per_unit' to be decimal
What should I do? Please help.
Thank you in advance

Related

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

How to copy varchar column with currency value to decimal in SQL Server?

I have a table with around 20K rows of data. One varchar column contains currency info in the format $xxx,xxx,xxx.00. I'd like to create an additional column that is of type decimal and copy the varchar data into it, obviously while stripping off the $ and ,'s.
I've played with cast and convert, mixed with trim and replace functions, but haven't been successful yet in getting this into a new column.
Because we're doing many future calculations on the data, we want it converted once instead of having to convert it for each calculation.
Any suggestions would be greatly appreciated!
Thank you in advance!
--Kent
Try replacing the $ and the ',' and then Convert. You should be Good
CONVERT(NUMERIC(28,4),REPLACE(REPLACE(stringColumn,'$',''),',',''))
SQL Fiddle here.
Select Cast(Replace(Replace(ColName, '$', ''), ',', '') as Money)

SQL Server 2005 decimal field behaviour

Ive create a simple table with a field x of type decimal(28,15)
I run the following query to insert a value:
insert into testtable values (1234567890123)
when I do a select from testtable I, field x has a value of
1234567890123.000000000000000
Is there a way to specify (perhaps in the table design) that sql server should NOT store the zero's after the decimal if its not required? i.e. when doing a select the data will be returned as
1234567890123
Also why does insert into testtable values (12345678901234) give the following error :
Arithmetic overflow error converting numeric to data type numeric.
Also why does insert into testtable values (12345678901234) give the following error :
You specified a total width of 28 with 15 digits after decimal point. That leaves at most 13 digits before decimal point.
Is there a way to specify that sql server should NOT store the zero's after the decimal if its no required?
Strictly speaking, "decimal" is a fixed precision format and thus any number will occupy exactly the same space as any other number (see MSDN).
Try the below query . it works fine ...
select cast(1234567890123.000000000000000 as decimal)

SQL Decimal Field Conversion to Binary

I have a question: in SQL Server there is a decimal data type.
I can convert this datatype to binary, so I can write query as
declare #deci decimal(18,2)
set #deci = 2.33
print cast(#deci as varbinary)
The result of this query is --
0x12020001E9000000
My question is what is the conversion rule for this conversion from decimal to binary..can anybody explain to me?

SQL 'float' data type, when output as XML, causes undesired float result

You can try simply:
table1: has a column1 of type 'float'
instead of
SELECT column1 from Table1; gives values as seen in table.
Say this returns 15.1
However, if you try
Select column1 from Table1
FOR XML PATH('Table1'), Root('SomeRoot'), TYPE
returns: 1.510000000000000e+001
Has anyone seen this, and how was this fixed?
thanks in advance :)
This is what you get when you work with floating point numbers. You can try this though:
SELECT CONVERT(varchar(100), CAST(column1 AS decimal(38,2)))
you will just need to adjust the precision on the decimal to fit your needs.
Also assuming MSSQL, the str function might fit your needs (MSDN):
select str(column1, 3,1)
It's not necessary to convert the float value into a string to solve this problem.
Just convert the float value into a decimal or a numeric type, with the precision you want.
SELECT CAST(column1 AS decimal(38,2))
or
SELECT CAST(column1 AS numeric(18,5))
When then number will be parsed in XML by Sql Server, it won't be in an exponential form.
This approach is obviously faster than a string conversion (that would occur twice).

Resources