Oracle Functions
Oracle functions are very handy utilities to help you massage a result set to conform to your requirements, whether you are selecting, inserting, deleting or updating.
Below is a list of some commonly used sql functions, with an example of each:
- add_months
- avg
- ceil
- chr
- concat
- count
- decode
- dump
- floor
- greatest
- initcap
- instr
- last_day
- least
- length
- lower
- lpad
- ltrim
- max
- min
- mod
- months_between
- next_day
- nullif
- nvl
- nvl2
- replace
- round
- rpad
- rtrim
- sqrt
- substr
- sum
- sysdate
- to_char
- to_date
- to_number
- translate
- trim
- trunc
- upper
add_months
Returns the supplied date with the requested number of months added to it.
If the supplied date has a day value which is larger than the maximum number of days
in the resulting month, the result is the last day of that month.
SYSDATE
——————–
25-dec-2007 23:41:50
SQL> select add_months(sysdate,1) “result” from dual;
result
——————–
25-jan-2008 23:41:52
avg
Returns the average of a series of numbers.
SALARY
———-
20000
30000
30000
10000
SQL> select avg(salary) “result” from employees;
result
———–
22500
ceil
Returns the smallest integer larger than the supplied argument.
Opposite of sql function: floor.
99.5 result
—- ———-
99.5 100
chr
Returns the character equivalent of the supplied integer.
result
——
ORACLE
concat
Returns the first argument concatenated with the second argument.
The result’s dataype will depend on the datatype of the arguments.
I prefer to use the two pipe symbols (||) to concatenate.
result
——–
database
SQL> select ‘data’||’base’ “result” from dual;
result
——–
database
count
Returns the number of rows as requested by the query, either all rows or only distinct values.
By specifying *, it will return all rows including duplicates and nulls.
If no rows match your query, count will return 0. (never null)
result
———-
14
SQL> select job “result” from emp;
result
———
CLERK
SALESMAN
SALESMAN
MANAGER
SALESMAN
MANAGER
MANAGER
ANALYST
PRESIDENT
SALESMAN
CLERK
CLERK
ANALYST
CLERK
14 rows selected.
SQL> select count(distinct job) “result” from emp ;
result
———-
5
SQL> select count(*) “result” from emp where job = ‘VP’;
result
———-
0
decode
The oracle decode function takes up to 255 argument sets and returns the relevant result depending on the value found.
This is similar to the ‘case’ statement in other languages.
The last argument is what will be returned if no match is found in the decode statement.
dn result
– ——
10 ten
20 twenty
30 thirty
40 other
dump
Returns a string representing the argument’s datatype code, byte length and internal representation.
Very handy command if you are hunting for unexpected or hidden characters in your data.
result
———————-
Typ=96 Len=3: 99,97,116
SQL> select dump(‘cat ‘) “result” from dual;
result
————————-
Typ=96 Len=4: 99,97,116,32
floor
Returns an integer equal to or 1 less than the argument.
Opposite of oracle function : ceil.
99.5 result
—- ———-
99.5 99
greatest
Returns greatest ranking argument out of a list of arguments.
The ranking is determined by the value of the numerical codes of the whole character string.
Opposite of oracle function : least.
result
———
oracle10G
initcap
Returns the string with each word’s first letter capitalized.
Similar to oracle functions : lower and upper
result
——————–
Oracle Database Tips
instr
Returns the first position of a string inside another string.
The 1st argument is the string to be searched.
The 2nd argument specifies the character or string to search for.
The 3rd argument allows you to specify from which character position to start searching.
The 4th argument allows you to specify which occurence of the search string to return.
result
——
2
SQL> select instr(‘package’,'a’,3,1) “result” from dual;
result
——
5
SQL> select instr(‘package’,'a’,1,2) “result” from dual;
result
———-
5
last_day
Returns the last day’s date of the supplied date’s month.
SYSDATE result
——————– ——————–
27-dec-2007 15:49:27 31-dec-2007 15:49:27
least
Returns the least or smallest ranking argument out of a list of arguments.
The ranking is determined by the value of the numerical codes of the whole character string.
Opposite of oracle function : greatest
result
——
oracle
length
Returns the length (as an integer) of the supplied argument.
result
———-
15
lower
Returns the argument in lower case.
Similar to oracle functions : upper and initcap
result
——
oracle
lpad
Returns the first argument, left-padded by the third argument, so that the complete string is as long as specified by the second argument.
If you do not supply the third argument, blank spaces are used for padding.
Similar to oracle function : rpad
result
———-
****oracle
SQL> select lpad(‘oracle’,10) “result” from dual;
result
———-
oracle
ltrim
Removes the second argument from the first argument.
If you do not supply the second argument, it will remove blank spaces.
Similar to oracle function: rtrim
result
———–
le database
SQL> select ltrim(‘ oracle’) “result” from dual;
result
——
oracle
max
Returns the largest of a series of numbers.
Similar to oracle function : min
GRADE
———-
1
2
3
4
5
SQL> select max(grade) “result” from salgrade;
result
———-
5
min
Returns the smallest in a series of numbers.
Similar to oracle function : max
GRADE
———-
1
2
3
4
5
SQL> select min(grade) “result” from salgrade;
result
———-
1
mod
Returns the remainder of the first argument divided by the second argument.
The result is floor-ed.
result
———-
15
months_between
This date-oriented oracle function returns the number of months between two dates as a fraction.
result
———-
10.483871
next_day
Returns date of the day of the week (supplied as the second argument) after the date of the first argument.
The second argument is specified as the name of a weekday.
result
——————–
06-jan-2008 00:00:00
nullif
Returns null if the first and second argument are equal, if they are unequal, it will return the first argument.
result
———-
SQL> select nullif(1,2) “result” from dual;
result
———-
1
nvl
Returns the second argument if the first argument is null.
Otherwise, the first argument is returned.
Similar to oracle function : nvl2
result
——–
no value
SQL> select nvl(‘some value’,'no value’) “result” from dual;
result
———-
some value
nvl2
This function is an extension to nvl by allowing you to specify a value to return if the argument to be checked is null.
So, nvl2 will return the third argument if the first argument is null and will return the second argument if the first argument is not null.
Similar to oracle function : nvl
result
——–
no value
SQL> select nvl2(‘some value’,'has value’,'no value’) “result” from dual;
result
———
has value
replace
Returns the first argument with every occurence of the second argument replaced with the third argument.
If the third argument is left out, the second argument is removed from the first argument.
If the second argument is null, then the first argument is returned unmodified.
result
————
up the creek
SQL> select replace(‘down in the creek’,'down in’) “result” from dual;
result
———-
the creek
SQL> select replace(‘down in the creek’,null,’up’) “result” from dual;
result
—————–
down in the creek
round (number)
Returns the first argument rounded to the number of places after the decimal point as specified by the second argument.
If the second argument is not specified, then the first argument is rounded to 0 places after the decimal point.
If the result is n.5, round will round up to the next integer, if the result is -n.5, it will round down to the next integer.
result
———-
8.35
SQL> select round(8.345) “result” from dual;
result
———-
8
SQL> select round(8.5) “result” from dual;
result
———-
9
SQL> select round(-8.5) “result” from dual;
result
———-
-9
round (date)
This will always return a date.
The format of the second argument determines the timeperiod it will round to, like YEAR, MONTH, DAY, HH and MI.
If you do not specify a second argument, the format defaults to day.
result
——————–
04-jan-2008 15:34:19
SQL> select round(sysdate) “result” from dual;
result
——————–
05-jan-2008 00:00:00
SQL> select round(sysdate,’YYYY’) “result” from dual;
result
——————–
01-jan-2008 00:00:00
rpad
Returns the first argument, right-padded by the third argument, so that the complete string is as long as specified by the second argument.
If you do not supply the third argument, blank spaces are used for padding.
Similar to oracle function : lpad
result
—————
star status ***
rtrim
Removes the second argument from the first argument.
If you do not supply the second argument, it will remove blank spaces.
Similar to oracle function : ltrim
result
——
oracle
sqrt
Returns the square root of the argument.
result
———-
4
substr
This very useful sql function returns the part of the first argument, determined by what you specify as the second and third arguments.
The second argument tells Oracle from which character position to start counting.
It can contain a positive or negative value.
The third argument determines the number of characters to select.
If you do not supply this argument, all characters until the end are selected.
Read up more about this very useful oracle function :
Oracle online documentation : SQL Reference, substr
result
——-
function
SQL> select substr(‘oracle functions’,8) “result” from dual;
result
——————
functions
sum
Returns the sum of a range of numbers.
SAL
———-
800
1600
1250
2975
1250
2850
2450
3000
5000
1500
1100
950
3000
1300
14 rows selected.
SQL> select sum(sal) “result” from emp;
result
———-
29025
sysdate
Returns the current date and time of the local database server’s operating system.
You will see the date format as it is specified by the NLS_DATE_FORMAT
database initialization parameter.
You can set NLS_DATE_FORMAT for your session to overwrite the database’s default value.
Alternatively, you can convert sysdate to a character datatype using the to_char function.
result
——————–
05-jan-2008 16:57:11
SQL> alter session set nls_date_format=’dd/mm/yyyy hh24-mi’;
Session altered.
SQL> select sysdate “result” from dual;
result
—————-
05/01/2008 16-58
SQL> select to_char(sysdate,’dd/mon/yyyy hh24:mi:ss’) “result” from dual;
result
——————–
05/jan/2008 16:58:36
to_char
Converts the argument to a character datatype, using the format specified by the second argument.
You can convert number, date and CLOB datatypes.
Read up more about format models here:
Oracle online documentation : SQL Reference, format models
result
——————–
$800.00
$1,600.00
$1,250.00
$2,975.00
$1,250.00
$2,850.00
$2,450.00
$3,000.00
$5,000.00
$1,500.00
$1,100.00
$950.00
$3,000.00
$1,300.00
SQL> select to_char(sysdate,’DD-MON-YYYY hh24:mi:ss’) “result” from dual;
result
——————–
05-JAN-2008 16:42:27
select to_char(clob_column) “result” from large_table where itemno = 469;
result
————————————————————————–
Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed
diam nonumy eirmod tempor invidunt ut labore et dolore magna
aliquyam erat, sed diam voluptua. At vero eos et accusam et
justo duo dolores et ea rebum. Stet clita kasd gubergren, no
sea takimata sanctus est Lorem ipsum dolor sit amet.
to_date
Converts a character string to a date datatype.
If the character string is already in the default format, you do not need to specify a format as the second argument.
The default format is specified by initialization parameter NLS_TERRITORY or can be set by NLS_DATE_FORMAT.
Read up more about format models here:
Oracle online documentation : SQL Reference, oracle function format models
PARAMETER VALUE
————————– —————————————-
NLS_DATE_FORMAT dd-mon-yyyy
SQL> select to_date(’01-jan-2008′) “result” from dual;
result
———–
01-jan-2008
SQL> select to_date(’01/01/2008′) “result” from dual;
select to_date(’01/01/2008′) “result” from dual
*
ERROR at line 1:
ORA-01843: not a valid month
SQL> select to_date(’01/01/2008′,’DD/MM/YYYY’) “result” from dual;
result
———–
01-jan-2008
to_number
Converts a character datatype to a number datatype
The second argument supplies the format needed to do the conversion.
Read up more about format models here:
Oracle online documentation : SQL Reference, format models
result
———-
800
translate
Returns a string which replaces all occurences of the characters in the second argument
with the characters of the third argument.
If the second argument contains more characters than the third argument,
the extra characters are removed from the result.
result
——–
December
SQL> select translate(‘September’,'Sept’,'Dec’) “result” from dual;
result
——–
December
trim
Returns the third argument with the leading, ending or both sides of the argument’s characters trimmed.
The first argument specifies which side to trim from.
Values can be ‘LEADING’,'TRAILING’ or ‘BOTH’.
If null, both sides are trimmed.
The second argument specifies which character to trim. If null, blanks are assumed.
The third argument takes the form : FROM < source > which can be a column name.
ENAME
———-
FORD
SQL> select trim(leading ‘F’ from ename) “result” from emp where ename = ‘FORD’;
result
———-
ORD
trunc(number)
Returns the first argument truncated to the number of decimal places determined by the second argument.
If the second argument is not specified, Oracle assumes 0.
result
———-
18.45
SQL> select trunc(18.456) “result” from dual;
result
———-
18
trunc(date)
Returns the date truncated to the day specified by the second argument’s format.
The format of the second argument determines the timeperiod it will truncate to, like YEAR, MONTH, DAY, HH and MI.
If you do not specify a second argument, the format defaults to DAY.
result
———–
05-jan-2008
SQL> select trunc(sysdate,’YEAR’) “result” from dual;
result
———–
01-jan-2008
upper
Returns the argument with all characters in upper case.
Similar to oracle functions : lower and initcap
result
————————–
ORACLE FUNCTIONS
Recommended reading for more information on oracle functions :
Posted: October 23rd, 2009 under SQL.
Tags: ceil, chr, function syntax, handy utilities, oracle functions, oracle top, rtrim, salary, sql functions


