Searching on Last Decimal Place - sql

In the SQL (SQL Server 2019) database I am working, the vendor has introduced a rounding bug and I'm trying to track down all instances of this issue.
On a decimal field with 10 decimal places eg. 42.5500000001, I want to search where only the last decimal place is not equal to zero. I'm struggling with the code on this one.
Any assistance is appreciated.

Select the last value in your column from the RIGHT and check to see if it’s not equal to zero.
select * from test_table where right(test_column, 1) <> 0
db<>fiddle here.

Related

How to get only 2 decimal places from a function?

I'm using a procedure to get some information from my database, and I have a value that I get from a function. That value comes with 5 decimal places, so I tried to get that value with only 2 decimal places doing this:
cast([dbo].[myFunction](param1, param2, param3)as decimal(20,2)) as Total
The code doesn't give me any error, but it comes with 5 decimal places instead 2.
Thanks in advance.
It's solved, my procedure has a union, and that was without the cast and sql assumes the larger number of decimal places, I had to force in the 2nd part of the union too.
Thank you all.

SQL Server : aggregate function doesn't work?

I have imported a price list from a csv to my SQL Server database. That has worked fine. But now some weird stuff. Table is named PRICE which includes a column (and some more) Endprice and a total of 761 rows. All datatypes are varchar(50).
SELECT MAX(Endprice)
FROM PRICE
When I want this simple SQL statement to show the highest price in the column, I get a wrong result. I don't know why.
I get 98,39 as a result, but that's definitively wrong, it must be 100,73.
Here you can see a part of the data:
And now the wrong MAX() result:
BUT when I'm using the MIN function I get the highest one!? The min is somewhere at ~50 (not shown in the screenshot part).
`
The resultset of SELECT Endprice FROM PRICE is correct. I am at my wit's end.
This is because your column is a varchar, so it is determining the min or max based on characters. The column should be a decimal or money type, so it sorts by the value of your number (instead of an alphabetic sort like you are getting now).
Alphabetic sort: 9 is more than 1, thus 98.39 is the max.
The reason is because price is a varchar().
Here are two solutions:
order by len(price), price
This works assuming that all the price values have the same structure.
Or:
order by cast(price as float)
If you could have non-numeric values (always a danger when storing numbers in the wrong data type):
order by (case when isnumeric(price) = 1 then cast(price as float) end)
Or better yet:
alter table alter column price money
Then you don't have to worry about having the wrong type for the column.
Your problem is Endprice columns is varchar(50), therefore it is comparing strings not numbers, which means that a 9>1 no matter what cames next of the first digit. You have to convert it to a number before the max!
Also you really should consider in doing what #a_horse_with_no_name suggested change your column into a number like column type.
This is a example on how you solve your actual problem
select max(cast(endprice as money)) from sample
See it here: http://sqlfiddle.com/#!3/767f6/1
Note that I used . as a decimal separator it will depend on your database language setup.

Removing numbers that are negative from SQL query

How do you remove negative numbers from SQL query result.
Example:
Select acquisitionprice-salesprice
From Han.trans
I'm trying to find out the Profit by subtracting two columns from a table and would like to have the negative numbers removed from the result
WHERE salesprice > acquisitionprice
probably is treated as the same as giorgos-betsos commented
WHERE acquisitionprice-salesprice >= 0
but reads a little simpler to me.

Sum function doesn't give sufficient decimal places with divide function

I have the following sql query that I am running:
select sum(cast(mkt_value as decimal(20,7)))/1204438043.37 from table1
and I get the following result which is correct but I need at least 10 decimal places not 6:
0.009347
If I run the following query:
select sum(mkt_value) from table1
I get 11258490.2400.
If I divide 11258490.24 by 1204438043.37 in excel I get 0.009347504674 which is the answer I'm looking for.
Please help me correct my SQL!!
Your cast is breaking this. It doesn't have enough space to give you more than six decimal places. What you're saying is literally "give me the result of this division with at most six decimal places", and then you're suprised the result only has six decimal places :)
The solution is either to omit the cast (if the data type is money, it's fine) or increase the scale of the decimal, eg. decimal(20, 11). The second parameter of the decimal type says the maximal amount of decimal places (-1) in the number. Also, consider only casting the result of the sum instead of all the items.
Note that most operations in MS SQL return a value of the same data type. So:
select 3 / 4; -- Returns 0
select cast(1000 as smallint) * cast(1000 as smallint);
-- Error - 1 000 000 is too big for smallint
The same thing happens in your sum and also in the division that happens right after it :)
You need to apply casting after calculating final value
select CAST (sum(mkt_value)/1204438043.37 as decimal(15,10)) from table1.
This means result would be having maximum 15 digits and mandatory 10 decimal places.
I would suggest the following query,Since your final decimal places are getting truncated.Hence a cast would be required to your final result set.
select
cast(cast (sum(mkt_value) as decimal(20,7))/cast (1204438043.37 as decimal(20,7)) as decimal(20,12)) from table1
Thank you..Let me know if this works

How to retrieve rows of a money column in sql with more than 2 digits of precision to the right of the decimal?

I am having difficulty figuring out how to select a set of rows from my table where the precision of the value of the row is greater than two digits to the right of the decimal point. I have no need for any of the values that are 2 digit precision i only want the ones with greater than 2 digit precision. The end result is that the values with greater than 2 digit precision need to be rounded to constrain the values to only 2 digit precision. The code that inserts the data has been corrected to only insert values with 2 digit precision but i need to fix the ones that aren't.
Hey there, is your goal to find these records, and then update them? below is what I'm imaging you would need to do :
DECLARE #smallmoney as money
set #smallmoney = 1.0097
SELECT #smallmoney as actualValue, ROUND(#smallmoney,2)
WHERE #smallmoney <> ROUND(#smallmoney,2)
As you can see, if the amount of money is already within two decimal points, the where condition will filter that record out.
Hope this works for you!
I hunted around for an answer to the same problem where some script I had written had created a money column to greater than 2 decimal places - like you I needed to go and find the records that were greater than 2 DP - couldn't find the answer on SO, found the below answer elsewhere though.
select ID, MoneyColumn from OurTable
WHERE FLOOR(MoneyColumn*100)!=MoneyColumn*100