Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

T-Sql Remove Decimal Point From Money Data Type

Given the constraint of only using T-Sql in Sql Server 2005, is there a better way to remove the decimal point from a money datatype than a conversion to a varchar (here implicitly) and then a replace of the decimal point?

Here is what I have currently.

SELECT REPLACE(1.23, '.', ''), REPLACE(19.99, '.', '')

Which returns the desired 123 and 1999, but I was wondering if there was a better way. Any thoughts?

like image 414
Pete Avatar asked Aug 06 '08 19:08

Pete


People also ask

How do you remove decimals from data labels?

Right click on one of the decimal value on the graph and go to format y/x axis and under number tab you have an option to make the decimal places to 0.

How do you remove decimals from a Dataframe?

round() function to round off all the decimal values in the dataframe to 3 decimal places. Output : Example #2: Use round() function to round off all the columns in dataframe to different places.

How do I remove a decimal point from a number in SQL?

There are various methods to remove decimal values in SQL: Using ROUND() function: This function in SQL Server is used to round off a specified number to a specified decimal places. Using FLOOR() function: It returns the largest integer value that is less than or equal to a number.


2 Answers

Multiply by 100 and then convert to an int.

like image 152
Yaakov Ellis Avatar answered Sep 28 '22 00:09

Yaakov Ellis


You should never ever use the money datatype to store monetary values. If you do any calculations you will get truncated results. Run the following to see what I mean

DECLARE
@mon1 MONEY,
@mon2 MONEY,
@mon3 MONEY,
@mon4 MONEY,
@num1 DECIMAL(19,4),
@num2 DECIMAL(19,4),
@num3 DECIMAL(19,4),
@num4 DECIMAL(19,4)

SELECT
@mon1 = 100, @mon2 = 339, @mon3 = 10000,
@num1 = 100, @num2 = 339, @num3 = 10000

SET @mon4 = @mon1/@mon2*@mon3
SET @num4 = @num1/@num2*@num3

SELECT @mon4 AS moneyresult,
@num4 AS numericresult

Output: 2949.0000 2949.8525

like image 20
SQLMenace Avatar answered Sep 28 '22 02:09

SQLMenace