KnowlegeZone.com, Share knowledge Gain knowledge
New Articles:
     

Home > SQL : Understanding Decimal / Decimal(p,s) Data Type

Article By: Saqib Khan   Date: 2/27/2009

Understanding Decimal / Decimal(p,s) Data Type

Sometimes in T-SQL we try the Round Function and it just does not do the job.

For Example we have follow:
1599.980475

and we just want to show 1599.98,  lets say we just want to capture 1599.98 (just two digits after the decimal).

To Achieve this we need to use Decimal Data type.

Decimal(p, s)

it accepts two parameter.

p = Precision, Which means total number of digits in a number, For Example 158.99 means Precision is 5.

s = Scale, Which means total number of digits after the Decimal point (Right Side of decimal).

So now if we have 1599.980475 and we just want 1599.98 then all we need id ...

Decimal(6, 2) --- 1599.95

why 6? thats because our output contains a total number of 6 digits, meaning our precesion must be less then or equall to six.

why 2? thats because we want to show two numbers after the decimal point.

This is how you can use it on a SQL Table's Column.

select cast(1599.989547 as decimal(6,2))

 

And here is a Link for offiial documentation:
http://msdn.microsoft.com/en-us/library/ms190476.aspx

Thank you for reading.

 - Saqib
  www.SaqibKhan.Com

 

 

 



Share |


No comments posted.
Post Comment or Solution   OR   Sign in using Google/Aol/Yahoo/Msn/OpenID
Your Name:  
Your Email:  
Comments:
 
Please Verify:
(Type Answer)