rudyflyer
asked on
SQL - Round a dollar value and update it in SQL
Hello,
I have a field in SQL Server 2000 titled "TotalAmount" and this is Money(8). I need to change the decimal places to 2 instead of 4.
For example, I have a value of 9.8895 in this field. I need to write an UPDATE statement that will round this value to 9.89.
How can I do that?
Thanks in advance.
I have a field in SQL Server 2000 titled "TotalAmount" and this is Money(8). I need to change the decimal places to 2 instead of 4.
For example, I have a value of 9.8895 in this field. I need to write an UPDATE statement that will round this value to 9.89.
How can I do that?
Thanks in advance.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
The Money datatype has 4 decimal places, so you will always see the two trailing zeros if you keep that datatype. If you want the column to only have 2 decimal places, you need to change the datatype to something like DECIMAL(16,2). Before doing that, you need to execute an update statement to round all the values to two decimal places as suggested above.
Changing the datatype and saving will automatically round the values, but no reason to take a chance, run an UPDATE statement first.
I'm not sure if you are looking to store data this way or just return data in this format. If you are looking to return data in this format, then you could use a convert() on select:
SELECT CONVERT(decimal(16,2),Tota lAmount) FROM table1
If updating:
UPDATE table1
SET TotalAmount = ROUND(TotalAmount,2)
I'm not sure if you are looking to store data this way or just return data in this format. If you are looking to return data in this format, then you could use a convert() on select:
SELECT CONVERT(decimal(16,2),Tota
If updating:
UPDATE table1
SET TotalAmount = ROUND(TotalAmount,2)
When you read the data, you can also use cast
select Cast( round(TotalAmount,2) as decimal(16,2) ) as "TotalAmount"
from yourtable
The update statement is still correct, though.
After doing the update, you can:
select Cast(TotalAmount as decimal(16,2) ) as "TotalAmount"
from yourtable
select Cast( round(TotalAmount,2) as decimal(16,2) ) as "TotalAmount"
from yourtable
The update statement is still correct, though.
After doing the update, you can:
select Cast(TotalAmount as decimal(16,2) ) as "TotalAmount"
from yourtable
in mysql there exist a truncate() function
mysql> SELECT TRUNCATE(-1.999,1);
-> -1.9
i dnt know if such function exist in MS SQL
mysql> SELECT TRUNCATE(-1.999,1);
-> -1.9
i dnt know if such function exist in MS SQL
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Arthur is correct. Not storing 4 digits to right of decimal to lead to greater problems.
ASKER
For example, the value will be 45.0000 or 49.9500.
I need it to be 45.00 or 49.95