How to get only 2 decimal places from a function? - sql

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.

Related

Searching on Last Decimal Place

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.

SQL Calculation to 2 decimal places

I've got a simple calculation (910 / 28 = 3.5) and I'm trying to perform this in a SQL query:
SELECT CONVERT(DECIMAL(5,2), (910 / 28),2) AS average
But the answer is coming out at 32.00, I'm obviously missing something simple could someone spare a moment to point out my error please?
Thanks,
C
Use this:
SELECT CONVERT(DECIMAL(5,2), (910.0 / 28)) AS average
By taking the quotient as 910.0 / 28 SQL Server will retain decimal precision. Then, make your cast to a decimal with two places. By the way, as far as I know CONVERT typically takes just two parameters when converting a number to decimal.
we can use this query for dynamic value from table:
SELECT CONVERT(DECIMAL(5,2), (cast (910 as decimal))/ 28) AS average
It will give the desire output
Unsure if this applies to your database, but in Trino SQL (a sort of database middleware layer), I find adding a decimal point followed by two zeros to any of two operands in this query (e.g., select 910.00/23 AS average or select 910/23.00 AS average) returns a non-integer value (39.57, instead of 39).
Adding 3 zeros after the decimal (select 910.000/23 AS average) returns a 3-decimal place result (39.565), and so on.
Try this query...
SELECT CAST(910 AS decimal(5,2)) / CAST(28 AS decimal(5,2)) as average
Try use this
select cast(round(910/28.0,2) as numeric(36,2)) AS average

Oracle MONTHS_BETWEEN to return a whole number

I have an Oracle Select statement, and inside it I have the following:
MONTHS_BETWEEN(CON.F_CON_END_DATE, TRUNC(SYSDATE)) MONTHSTOEXPIRE,
The aim of that code is to get me the number of months between two dates. It does work, but it returns them in a way that is breaking my VB.NET code. Something about an overflow exception!
It returns 1.83870967741935 and I expect a 2?
What can I do to make my query return the nearest whole number for the MONTHSTOEXPIRE value?
I'm guessing you simply need to use the round() function?
ROUND(MONTHS_BETWEEN(CON.F_CON_END_DATE, TRUNC(SYSDATE)), 0) MONTHSTOEXPIRE
It works on my Oracle.
Let me know if it works on yours!
You can try
Math.Round,
Rounds a value to the nearest integer or to the specified number of fractional digits.
Math.Ceiling
Returns the smallest integer greater than or equal to the specified number.
Math.Floor.
Returns the largest integer less than or equal to the specified number.
use like this
round(MONTHS_BETWEEN('29-Jun-2014', TRUNC(SYSDATE))) MONTHSTOEXPIRE
since you cannot always get exact month count, you can get the exact value and round it to the nearest integer

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