Shortest method to extract decimal value

One of the questions asked in a forum.  “What are the different methods to extract decimal part from a number?”.

There can be many methods. Two simple methods are as shown below

1 CAST to INT and subtract from original number

declare @amount decimal(12,2)
set @amount=1200.75
select @amount-cast(@amount as int) as decimal_value

2 Use Modulus operator

declare @amount decimal(12,2)
set @amount=1200.75
select @amount % 1 as decimal_value

The result is

decimal_value
---------------------------------------
0.75

Using modulus operator is the shortest method of all methods that you can use

Advertisements

One thought on “Shortest method to extract decimal value

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s