Issue in rounding off the values extracted from Teradata SQL assistant - sql

I need to round off couple of fields that I extract from Teradata SQL assistant.
Currently I am using CAST(Field1 as numeric(20,2)) as Field1
18.529 is rounded to 18.53 but 36.425 is rounded to 36.42 instead I am expecting 36.43
How can this be achieved?

The rounding rules for CASTs depend on a global setting, RoundHalfwayMagUp in dbscontrol.
You might try the ROUND function which defaults to the rounding rules you prefer:
ROUND(36.425,2)

I found an old post on a forum here which states that the RoundHalfwayMagUp controls whether .5 rounds up or down. See the docs for more info

because in 36.425, 5 is near to 0 not to 10,
if you put 36.426 it will round of to 36.43
Round :
It will work like below
between 0 and 5 > increment by 0( replaced )
between 5 to 10 > increment by 1 ( replaced)

Related

How to use bigquery round up results to 4 digits after decimal point?

We don't have decimal data type in BigQuery now. So I have to use float
But
In Bigquery float division
0.029*50/100=0.014500000000000002
Although
0.021*50/100=0.0105
To round the value up
I have to use round(floatvalue*10000)/10000.
Is this the right way to deal with decimal data type now in BigQuery?
Depends on your coding preferences - for example you can just use simple ROUND(floatvalue, 4)
Depends on how exactly you need to round - up or down - you can respectively adjust expression
For example ROUND(floatvalue + 0.00005, 4)
See all rounding functions for BigQuery Standard SQL at below link
https://cloud.google.com/bigquery/docs/reference/standard-sql/functions-and-operators#rounding-functions
Note that this question deserves a different answer now.
The premise of the question is "We don't have decimal data type in BigQuery now."
But now we do: You can use NUMERIC:
SELECT CAST('0.029' AS NUMERIC)*50/100
# 0.0145
Just make your column is NUMERIC instead of FLOAT64, and you'll get the desired results.
Rounding up in most SQL dialects is not a built-in function unless you're fortunate enough to be rounding up to an integer. In this case, CEIL is a quick and reliable solution.
In the case of rounding decimals up, we can also leverage CEIL, albeit with a couple of additional steps.
The procedure:
Multiply your value to move the last decimal to the tenths position. Ex. 18.234 becomes 1823.4 by multiplying by 100. (n * 100)
Use CEIL() to round up to the nearest integer. In our example, CEIL(n) = 1824.
Divide this result by the same figure used in step 1. In our example, n / 100 = 18.24.
Simplifying these steps leaves us with the below logic:
SELECT CEIL(value * 100) / 100 as rounded_up;
The same logic can be used to round down using the FLOOR function as such:
FLOOR(value * 100) / 100 AS rounded_down;
Thanks to #Mureinik for this answer.

SQL: How to format decimals, including ones less than 1

This feels like a REALLY basic question buy I have been looking around for about an hour and have yet to find a straight answer.
I have a decimal number, it represents a weight, I want it rounded and formatted in a way that is considered normal.
1.0000 to 1.00
.1900 to 0.19
-.1900 to -0.19
Right now I'm getting rid of the trailing zeros by casting as a decimal and rounding them off
CAST(TLI_Amount as decimal(18,2))
Now heres the kicker. I don't know what version of SQL I'm on, but it must be really old. Old enough that it doesn't recognize FORMAT as a function. Which is pretty much what everyone says to use. That or the leading zero get put in front of everything, but it needs to only be there for numbers < 1
So how can I get my decimals < 1 to read as 0.xx, like any normal human readable number should be.
SQL Anywhere does support ##version so perhaps that's not what you're working on. But if you are, you can look up the documentation on the str() function here. In a nutshell:
select str( 1.2345, 4, 2 )
-> 1.23
select str( 0.1234, 4, 2 )
-> 0.12
select str( -0.1234, 5, 2 )
-> -0.12
There is also a round function.
Well, how about just adding the zero in. I think SQLAnywhere uses + for string concatenation:
SELECT (CASE WHEN TLI_Amount < 1 THEN '0' ELSE 1 END) +
CAST(CAST(TLI_Amount as decimal(18, 2)) as VARCHAR(255))
)
Note: This assumes that TLI_Amount is positive.

postgresql round division

I want to divide values and round them up to 8 decimal places but i found that some divisions return in scientific notation.
How can i always get round division without scientific notation?
select round( 123/100000000::decimal, 8 )
returns 0.00000123 as expected.
select round( 1/100000000::decimal, 8 )
returns 1e-8 but... i was expecting 0.00000001
How can i round 1/100000000 to 8 decimal places and return 0.00000001 ?
sql fiddle: http://sqlfiddle.com/#!15/9eecb7db59d16c80417c72d1e1f4fbf1/1534
Thanks for help.
best,
Actually, when I try your code in Postgres 9.3.4 using pgAdmin, both return the values you want. The values are not in exponential notation. Hence, I suspect this is an issue with your application, not the database.
An easy way to check is to put the value as a string:
select round( 1/100000000::decimal, 8 )::text
This should not return exponential notation.
Probably second case go beyond precision range 8.
You can check this Question Selecting floating point numbers in decimal form
Also what version are you working with because in pgAdmin 9.2 I get a different result

Oracle SQL displaying 100% as 0%

I have a query that shows percentage of a job being to completion. Here is my query:
NVL(substr(countprocess_locations.count_attempt_id)/count(processes.process_id),2,2),0)||'%' as "Percentage Complete"
Everything has worked fine and beautifully, BUT I recently had a count hit 100% mathematically, but in the output of the query it says 0. I've tried playing with 2,2),0 and no difference so far :/
Any help would be greatly appreciated.
Sample
With a working example breaking it down.
159 / 180 = 0.883333
SUBSTR first converts it to text, when doing so Oracle drops the leading zero
.883333
SUBSTR then takes two characters starting at the second
.*88*3333
Then you concatenate '%' to it
88%
When you have 100% it flows such:
2000 / 2000 = 1
Converts to text
1
Takes two characters starting at the second
NULL
Then your NVL returns 0.
Instead of SUBSTR you should * 100 and then ROUND or if you really want to just drop the digits instead of rounding use TRUNC.
NVL(ROUND((countprocess_locations.count_attempt_id)/count(processes.process_id))*100,0),0)||'%'

Oracle ceil for decimal numbers

When rounding up to 2 decimal places, the value 4.01132141 would be rounded to 4.02 because it exceeds 4.01.
How can you do this in PL/SQL?
One way would be to do ceil(value*100)/100, but that seems inelegant. Not sure there's any way to make round behave the way you want.
The function to 'round up' is CEIL, but it generates an integer.
The function to 'round down' is FLOOR, but it too generates an integer.
The function to 'round nearest' is ROUND, and it allows you to specify a number of decimal places (dp).
Note that CEIL rounds to an integer; to round to 2 dp, you'd have to multiply by 100, use CEIL, and divide by 100.
To get the answer reasonably directly, use:
ROUND(value+0.005, 2)
This works because, for the example data of 4.01132141, the value passed to ROUND is 4.01632, and when rounded to 2 dp, that becomes 4.02. If the value started as 4.0593, say, then the value passed to ROUND would be 4.0643, which when rounded to 2 dp becomes 4.06, as required.
There are a couple of tricky bits there:
If the number of dp varies, the value to be added (0.005 in the example) varies. You could create a table to hold the number of decimal places in one column and the rounding value to add in the other. Alternatively, you could use an expression with powers of 10, etc.
Deciding on the correct behaviour for negative numbers. Does -4.01132141 become -4.02 or -4.01? You might need to play with SIGN and ABS functions to get that to work as you want.
I faced the same issue and came up with the following statement, it has worked fine so far.
select 4.01132141+(mod((ceil(4.01132141)-4.01132141)*1000,10)/1000) from dual
select 4.01132141, CEIL(4.01132141*100)/100 from dual
You can use this for Rounding up in PLSQL:
ROUND( UrNo+ (5 / POWER(10, DecimalPlaces + 1)) , DecimalPlaces)