Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Query for decimal places 2

Status
Not open for further replies.

Bluejay07

Programmer
Joined
Mar 9, 2007
Messages
780
Location
CA
Hi,

I am looking for a query where I need to determine if a specific decimal place is greater than zero.

Example.
3.450
4.835
2.764
9.882

If we take the above numbers, I would want to query based on the third decimal position.

The results should be:
4.835
2.764
9.882

Any help would be greatly appreciated.
 
Here's how I would approach this....

First multiply the number by 100. Then, take the floor of the value (this will round down to the nearest integer). Then, divide by 100.

In the first example, 3.450

Multiple by 100 = 345.0
Floor = 345.0
divide by 100 = 3.45

Since this is the same number you started with, filter it out.

Now for 4.835
Multiply by 100 = 483.5
Floor = 483
divide by 100 = 4.83

Since this is NOT the same number, include in your results.

Full Example:

Code:
Declare @Temp Table(Data Decimal(20,3))

Insert Into @Temp Values(3.450)
Insert Into @Temp Values(4.835)
Insert Into @Temp Values(2.764)
Insert Into @Temp Values(9.882)

Select Data
From   @Temp
Where  Data <> Floor(Data * 100) / 100


-George

"The great things about standards is that there are so many to choose from." - Fortune Cookie Wisdom
 
Code:
SELECT *
       FROM YourTable
WHERE CAST(YourField*1000 AS int) % 10 > 0
NOT TESTED!

Borislav Borissov
VFP9 SP2, SQL Server 2000/2005.
 
Thank you both for your reply.

Forums like this are great since a different approach to a problem can be found from different people.

I have quickly tested both methods and they returned the same amount of rows (which is a great sign).

Thank you again for a solution.

A star to you both.

Have a great day.
 
With one specification:
George's suggestion is optimizable (if you have index on that field :-))

Borislav Borissov
VFP9 SP2, SQL Server 2000/2005.
 
I don't think so. I use the data column in a formula on the right side of the inequality check. I was actually thinking that your query would be slightly faster. You'd probably have to try pretty hard to time the difference though.

-George

"The great things about standards is that there are so many to choose from." - Fortune Cookie Wisdom
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top