views:

4759

answers:

6

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?

+6  A: 

Multiply by 100 and then convert to an int.

Yaakov Ellis
A: 

Could you be a little more specific about the use case? Removing the decimal point from the representation is a little unusual given that you'll lose all information about the scale. Are you assuming that there will always be two digits? If so, you could simplify multiply by 100 and then round before converting to a string.

Curt Hagenlocher
A: 

@Curt Hagenlocher The data value would be ultimately used in a system that does not want decimal points (most file feeds operate this way). Thanks to you also for the x100 suggestion.

@Yaakov Ellis Sometimes the simple answers can be the most elusive. Thanks.

Pete
A: 

Keep in mind that the money data type can have up to 4 digits past the decimal. Values with more than two digits might not work as expected for either your original solution or the x100 trick.

Lance Fisher
+3  A: 
SQLMenace
A: 

here is the magic DataFormatString="{0:c0}

this will remove the decimal places

The original constraint was that it could only be in T-Sql. DataFormatString is a .Net thing.
Pete