Home > Ask the SQL Server Experts > SQL Server Expert Archive Questions & Answers > Returning values with more than two digits to the right of the decimal point
Ask The SQL Server Expert: Questions & Answers
EMAIL THIS

Returning values with more than two digits to the right of the decimal point

EXPERT RESPONSE FROM: Bianca Blount

Pose a Question
Other SQL Server Categories
Meet all SQL Server Experts
Become an Expert for this site


Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us   


>
QUESTION POSED ON: 13 March 2002
How do I write a WHERE clause that will return only values that have more than two digits to the right of the decimal point (ie. 27.543)? The data type is decimal, length 9, precision 13, scale 6. This is a typical situation when you want a dollar figure that is exactly two decimal place to the right of the decimal. I hope this is not too easy.

>
EXPERT RESPONSE

When inputting a number as a decimal, MS SQL Server pads the end of the number with 0s to equal the number of places declared in the scale. In other words, 27.543 is really 27.543000. To get around this, find the number of digits between the decimal point and the first zero.

Example:

declare @tbl_decimals table( 
        dec_column dec(13, 6)
) 

insert into @tbl_decimals 
values(123.1) 
insert into @tbl_decimals 
values(1234.12) 
insert into @tbl_decimals 
values(12345.123) 
insert into @tbl_decimals 
values(1234.1234) 

select * 
from @tbl_decimals 
WHERE len(left(right(dec_column, 6), 
   charindex('0', right(dec_column, 6)) - 1))
   > 2 
/* The number "6" should be replaced with the number 
indicated as the scale dec(precision, scale)*/ 

For More Information


Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us   


RELATED CONTENT
SQL Server Expert Archive
Applying LDF file to MDF without turning it to .BAK first?
Any way to alias a call to functions?
"Max connections reached" error
Application freezes for 30 seconds on VPN
Adding column and changing datatype
Converting varchar columns to text data type
Adding new column to table that is part of replication
Any issues with linking 7.0 and 2000 databases?
'Format' in SQL Server
Accessing SQL Server databases on an intranet with VB.NET

RELATED RESOURCES
2020software.com, trial software downloads for accounting software, ERP software, CRM software and business software systems
Search Bitpipe.com for the latest white papers and business webcasts
Whatis.com, the online computer dictionary



Search and Browse the Expert Answer Center
Search and browse more than 25,000 question and answer pairs from more than 250 TechTarget industry experts.
Browse our Expert Advice



SQL Solutions - SQL Database Design
HomeNewsTopicsITKnowledge ExchangeTipsAsk the ExpertsMultimediaWhite PapersIT Downloads
About Us  |  Contact Us  |  For Advertisers  |  For Business Partners  |  Site Index  |  RSS
SEARCH 
TechTarget provides enterprise IT professionals with the information they need to perform their jobs - from developing strategy, to making cost-effective IT purchase decisions and managing their organizations' IT projects - with its network of technology-specific Web sites, events and magazines.

TechTarget Corporate Web Site  |  Media Kits  |  Reprints  |  Site Map




All Rights Reserved, Copyright 2005 - 2008, TechTarget | Read our Privacy Policy
  TechTarget - The IT Media ROI Experts