Login or Sign Up to become a member!
LessThanDot Sit Logo

LessThanDot

Data Management

Less Than Dot is a community of passionate IT professionals and enthusiasts dedicated to sharing technical knowledge, experience, and assistance. Inside you will find reference materials, interesting technical discussions, and expert tips and commentary. Once you register for an account you will have immediate access to the forums and all past articles and commentaries.

LTD Social Sitings

Lessthandot twitter Lessthandot Linkedin Lessthandot friendfeed Lessthandot facebook Lessthandot rss

Note: Watch for social icons on posts by your favorite authors to follow their postings on these and other social sites.

Your profile

Search

XML Feeds

Google Ads

« Interview With Rod Colledge About The Book SQL Server 2008 Administration in ActionDerived Column and the Data Flow Task »
comments
Rate Post:
submit to reddit Digg!FacebookDotnetkicks

When you don't specify the precision and scale for your decimal data types, SQL Server will use its own default values, which is probably NOT what you want. In fact, the default precision and scale values are 18,0. If you want a whole number data type, use bigint, int, smallint, or int. If you want fractional numbers use DECIMAL, but ALWAYS specify the precision and scale.

For example:

  1. DECLARE @Blah DECIMAL  
  2.  
  3. SET @Blah = 65.00  
  4.  
  5. SELECT SQL_VARIANT_PROPERTY(@Blah, 'BaseType') AS [Base Type],  
  6.     SQL_VARIANT_PROPERTY(@Blah, 'Precision') AS [PRECISION],  
  7.     SQL_VARIANT_PROPERTY(@Blah, 'Scale') AS [Scale]

When you run the above code, you will see:
DECIMAL, 18, 0

As you can see, the default precision is 18 and the scale is 0. Most developers will choose to use an Integer (or bigint, smallint, tinyint) if they are working with whole numbers, and a decimal when working with fractional numbers. Unfortunately, the default precision and scale for the decimal data type results in the equivalent of an integer. In fact, the only difference between an integer and a Decimal(18,0) is the range of numbers that can be stored and the size (memory) used to hold the value.

               Storage Size            Minimum Value               Maximum Value
               ------------    --------------------------  -------------------------
Decimal(18,0)       9            -999,999,999,999,999,999    999,999,999,999,999,999
bigint              8          –9,223,372,036,854,775,808  9,223,372,036,854,775,807
int                 4                      –2,147,483,648              2,147,483,647

About the Author

George has been developing software professionally for 19 years, first for the department of defense, and then for various other companies. In 1998, George started his software company, Orbit Software, specializing in School Bus Transportation software. His specialty is refining SQL Server queries to deliver optimal performance.
Social SitingsTwitterLTD RSS Feed
1613 views
submit to reddit Digg!FacebookDotnetkicks

Comments and Feedback

3 comments

Comment from: SQLDenis [Member] Email
*****
Nice one George....and of course declare @Val varchar is another one that people do all the time
10/09/09 @ 08:00
Comment from: George Mastros [Member] Email
Thanks Denis.

I already blogged about the varchar/nvarchar problem here:

http://blogs.lessthandot.com/index.php/DataMgmt/DataDesign/sql-server-stored-procedure-with-nvarcha
10/09/09 @ 08:24
Comment from: Naomi [Member] Email
*****
Funny - turned the question into a blog. A good one!
10/09/09 @ 09:05

Leave a comment


Your email address will not be revealed on this site.

Your URL will be displayed.
(Line breaks become <br />)
(Name, email & website)
(Allow users to contact you through a message form (your email will not be revealed.)