How to add decimal to int? - sql

How can I add a decimal to an INT.
for example with the data below:
12452 into 124.52
500 into 5.00
1659865 into 16598.65
I have try this query but this is only for decimal :
select convert(decimal(10,2),sum(balance)) as Balance
from member

Just divide by 100:
select convert(decimal(10,2),sum(balance)/100.0) as Balance
from member
Converting doesn't change the actual value* - it just changes the data type, so 500 (int) becomes 500.00 (decimal(10,2))
*Unless the conversion itself changes the value due to a decrease in scale or precision (e.g. converting 1234.56 to decimal(5,1) would result in a value of 1234.6)

Related

SQL Server ROUND not working

I have a table where one column is Price (decimal(18,9)) and another Volume (bigint).
I am multiplying both values and then applying round function but nothing works.
I want it to be 2 decimal place precision. How to do it?
SELECT
CAST((Price * Volume) AS decimal(38,2)) test1,
ROUND((Price * Volume), 2) 'VolumePrice',
CONVERT(DOUBLE PRECISION, (Price * Volume)) 'test2'
FROM a
Table values are something like this:
Price Volume
-------------------------
63.380000000 131729
63.380000000 61177
44.860000000 246475
44.860000000 246475
44.860000000 63937
97.990000000 84620
191.650000000 438821
I want to simply multiply the price by the volume, to get a total value amount.
ROUND() just changes the decimal value up or down, doesn't change the data type precision.
What you want is to convert to DECIMAL with a scale of 2.
SELECT
CONVERT(DECIMAL(18,2), Price * Volume) AS DecimalConversion
FROM
A
Converting a decimal of higher scale (Price * Volume) to a lower one will automatically round the last digit:
SELECT
CONVERT(DECIMAL(18,2), '1.901999'), -- 1.90
CONVERT(DECIMAL(18,2), '1.909999'), -- 1.91
CONVERT(DECIMAL(18,2), '1.905999'), -- 1.91
CONVERT(DECIMAL(18,2), '1.904999') -- 1.90
When an operator combines two expressions of different data types, the rules for data type precedence specify that the data type with the lower precedence is converted to the data type with the higher precedence
source: MSDN docs
In SQL Server precedence order for data types in question is :
decimal
bigint
So bigint is converted to implicitly converted to decimal.
If you need your desired results you should simply do
SELECT
VolumePrice= cast(Price * Volume as decimal(18,2) )
FROM a
See working demo

SELECT $ (dollar sign)

I have good experience in SQL Server,
But suddenly I discovered this strange SELECT command
SELECT $
or
SELECT $ FROM tableName
All the time it returns zero scalar value (0.00),
or a new column with all values of 0.00
What is that?
When SQL Server comes across your $ sign, it automatically converts it into a money data type. Because you don't have an explicit value after the dollar sign, SQL Server is assuming 0.00. From MSDN:
When converting to money or smallmoney, integers are assumed to be monetary units. For example, the integer value of 4 is converted to the money equivalent of 4 dollars (for us_english, the default language). Numbers to the right of the decimal in floating-point values are rounded to four decimal places for money values. Expressions of data types char or varchar that are being converted to an integer data type must consist only of digits and an optional plus or minus sign (+ or -). Leading blanks are ignored. Expressions of data types char or varchar converted to money can also include an optional decimal point and leading dollar sign ($).
After a little messing around, I've figured since this happens no matter what currency symbol is used, SQL server is implying that the field is a currency field.
If you add numbers after the currency symbol, in this case a dollar so:
SELECT $4
SQL server will return 4.00
So SQL Server is taking the use of $ and assuming we want to create a field with the MONEY datatype and as we haven't entered a value after the currency symbol, SQL Server assumes the value is 0, though in my opinion this should return NULL.
You can prove out that SQL Server is treating it as a money type:
select $ as n into #x
exec tempdb..sp_help '#x'
sp_help will output (among other things):
Column_name Type Computed Length Prec Scale Nullable TrimTrailingBlanks FixedLenNullInSource Collation
-------------- ------- ----------- --------- ------- -------- ----------- --------------------- ----------------------- ------------
n money no 8 19 4 no (n/a) (n/a) (null)

Money formatted numbers without using Money data type

I could cast the number to money format and then convert to varchar, but that will give me max 4 decimal places. I need upto 5 decimal places. How can I do it?

Oracle: Decimal and rounding

I have a value in the table 0.0821, and I want to convert it to 8.21.
My SQL query is to_char(round(prdll_yr2.rate_chg_pct_qty *100 ),'9.99')
But it returns 8.00 instead of 8.21.
to_char(round(0.0821 *100,2 ),'9.99')
to_char(round(prdll_yr2.rate_chg_pct_qty *100,2 ),'9.99')
you're missing the number of decimals to display on the round... it will default to 0 if omitted
or for an example:
select to_char(round(0.0821 *100,2 ),'9.99') from dual;
Results in: 8.21
select to_char(round(0.0821 *100),'9.99') from dual;
Results in: 8.00
----------------------------GIVEN NEW INFORMATION:---------------------------
to_char(round(0.0821 *100,2 ),'9,999.99')
Adjust the 9,999.99 format to be equal to the scale and precision of the allowed in the database. so if your value is Numeric(9,5) this implies 4 leading numbers followed by 5 decimal places. Since you're multiplying by 100 the largest value you could have is 6 positions before the decimal so format of 999,999.99 and the 3rd decimal would be "Rounded"

Converting float to decimal in SQL Server 2008

I have a view which needs to return type decimal for columns stored as float.
I can cast each column to decimal as follows:
, CAST(Field1 as decimal) Field1
The problem with this approach, is that decimal defaults to 18,0, which automatically rounds the float columns to 0. I would like to keep a precision of up to 12 decimal places.
However, if I do this:
, CAST(Field1 as decimal(12,12)) Field1
I get a runtime error:
"Arithmetic overflow error converting float to data type numeric"
the float column is defined as length: 8 Precision: 53 in the table. I can not modify anything about the table.
What's the proper way to cast it as decimal w/out losing decimal precision?
12, 12 means no digits before the decimal separator: 12 digits in total, 12 of them being after the period.
Use a more appropriate range, say:
DECLARE #var FLOAT = 100
SELECT CAST(#var as decimal(20,12))
which gives you 8 digits before the separator, or adjust it as needed.