how to round off to next 10 in oracle? - sql

I need to find the next 10 digit number in a query.
I try to use round(n,-1) but it rounds off to nearest 10 digit but I need next 10 digit.
Please help me.
select round(5834.6,-1) from dual
gives 5830 but i need 5840

select ceil(5834.6/10)*10 from dual

Then add "5":
select round(5834.6 + 5,-1) from dual

A generic solution - Use MOD to find the last 10th place and then add 10 to the result.
select (5834.6 - MOD(5834.6,10)) + 10 from dual;
If you need the next 8th place, just replace any "10" with "8".

Select ceil(5834.6/10)*10 from dual;

Related

How would I use regular expression to extract words outside of brackets in Oracle?

I want to extract the value outside of brackets from a column.
Query:
select dwn_notes from down_time;
Query Result:
4 hour scheduled downtime (dball, 01/04/2019 09:14)
4 hour scheduled downtime (dball, 01/04/2019 09:14)
4 hour scheduled downtime (dball, 01/04/2019 09:14)
4 hour scheduled downtime (dball, 01/04/2019 09:14)
Based on your sample data, you can just use instr to locate the first open paren, and substring that out:
select dwn_notes,
substr(dwn_notes,1,instr(dwn_notes,'(') -1)
from
down_time
Fiddle
You can use REGEXP_REPLACE() to capture the part of the string before the parenthesis in a group and the part after the group, then return just those groups. Make sure to use a good set of test data, including values you don't expect.
with down_time(id, down_notes) as (
select 1, '1 hour scheduled downtime (dball, 01/04/2019 09:14)' from dual union all
select 2, '2 hour scheduled downtime (dball, 01/05/2019 09:14) with more text after' from dual union all
select 3, '' from dual union all
select 4, '(dball, 01/04/2019 09:14)' from dual union all
select 5, '5 hour scheduled downtime' from dual
)
select id, trim(regexp_replace(down_notes, '^(.*?)\s*\(.*\)\s*(.*?)$', '\1 \2')) down_notes
from down_time;
Be advised that if REGEXP_REPLACE() does not find a match, the string passed in will be returned. This is different from REGEXP_SUBSTR(), where it returns NULL if it can't find a match. You may have to allow for this in returned values.
this will work:
select regexp_replace(down_notes,'[(]{1}(.)*[)]{1}','') from Table1;
check fiddle:http://sqlfiddle.com/#!4/b52fd/6
Considering the part of the string upto the opening parentheses is enough for your case :
select regexp_substr(dwn_notes,'([^\()])+') as dwn_notes
from dwn_time;
If you need everything outside of the paretheses, then consider :
select concat(
regexp_substr(dwn_notes,'([^\(.*\))])+'),
regexp_substr(dwn_notes,'([^\(.*\))])+$')
) as dwn_notes
from dwn_time;
Demo

How to extract first number after decimal point in value

I have an age column which calculates age for each member in my report.The output is a whole number followed by a decimal point and numbers. I would like the first number only right after the decimal point .
I tried trunc but it gives me everything before the decimal and then the number I want after .Then I tried to trunc with a call out with a comma and it doesnt work.
trunc(age,',')
Example -
age 15.7
expected output 7
Here the mathematical answer
take the decimal part by susbriacting the whole part (trunc).
multiply by 10 and take the whole part
.
with age as (select 15.7231 age from dual)
select trunc(10*(age-trunc(age))) dp1 from age
DP1
----------
7
try like below
select substr(to_char(15.7,'9999.0'),-1,1) as col from dual
it will return 7
Multiply by 10, trunc it and take the remainder of the division by 10.
with age as (select 15.7231 age from dual)
select mod(trunc(10*age), 10) dp from age
Output:
DP
--
7

oracle SQL - show a number on multiple blank digits

So I'm having this problem , basicly I have a number 350 and I need to show it on 10 digits like this 350$$$$$$$ (I need to complete the number value with the character '$'). Thanks.
select rpad('350',10,'$') from dual
you can concatenate the values as $ is not number for example
select 350 ||'$' from dual

How to select list of next 30 dates for an input date parameter in oracle pl/sql or sql?

I am trying to select next 30 dates for an input date parameter, i.e. if I enter 2/3/2016 I should be able to select next 30 days. Is there a way to do it in oracle ?
The expected output is :
3/3/2016
4/3/2016
5/3/2016....
Try the following, replacing sysdate with your start date:
select trunc(sysdate) + level
from dual
connect by level <= 30
This is based on hierarchical queries and on the fact that adding a number to a date means adding days.
LEVEL is a pseudo-column, disposable where you use CONNECT BY, which indicates, as it seems, the level in recursion; so, the first occurrence has level 1, then 2, and so on.
Here are some more details on syntax and on the way this implements recursion.
Given that we have recursion on DUAL, with no conditions ( no PRIOR clause used), we use LEVEL both to sum 1, 2, 3 ( + LEVEL) days and to limit the number of recursione levels we need ( <=30)

trunc and round function in sql

Is trunc and round the same with negative arguments?
SQL> select round(123456.76,-4) from dual;
ROUND(123456.76,-4)
-------------------
120000
SQL> select trunc(123456.76,-4) from dual;
TRUNC(123456.76,-4)
-------------------
120000
No, behavior depends on the value of the significant digit (the 3rd digit (the 3) is the significant one in your case, as it is below 5 round and trunc do the same )
try select trunc(125456.76,-4) from dual (result is 120000) vs select round(125456.76,-4) from dual (result is 130000). Now when the significant digit is 5 (or higher) the results of trunc and round differ.
ROUND is related to round figure of given value.
TRUNC is related to truncation of given values.
In round case of given example, four places till 4th place prior to decimal point padded with 0.
But in trunc case, four places till 4th place prior to decimal point replacd with 0.
TRUNC - depending on second parameter it returns specified decimal places that is specified in second argument. Ex:
trunc(25.656) -- 25
trunc(25.656, 1) -- 25.6
trunc(25.656, 2) -- 25.65
ROUND - round off the given number to the nearest value. Ex:
round(66) -- 70
round(64) -- 60
round(65.7) -- 66
round(65.3) -- 65