You are on page 1of 5

Oracle Built in Functions

There are 2 types of functions in Oracle.


1. Single Row Functions
2. Group Functions

Single Row Functions


Single Row or Scalar Functions return a value for every row that is processed in a query.
Group Functions
These functions group the rows of data based on the values return by the query. The group
functions are used to calculate aggregate values like total, average, minimum and maximum.

Ex:
SUM(X), MIN(X), MAX(X), AVG(X), COUNT(X)

Single Row Functions


These are four types of single row functions.
1. Numeric Functions
2. Character (Or) Text Functions
3. Date functions
4. Conversion Functions

1. Numeric Functions
These are the functions that accept numeric input and return numeric values. These include five
functions.

1. ABS(X)
2. CEIL(X)
3. FLOOR(X)
4. ROUND(X)
5. TRUNC(X)

1. ABS(X)
Return the absolute value of the number x.

Ex:
select abs(4), abs(-4) from dual;
o/p: 4 4

2. CEIL(X)
Returns the integer value that is greater than (Or) equal to the number x.

Ex:
select ceil(2.83), ceil(2.49), ceil(-1.6) from dual;
o/p: 3 3 -1

3. FLORR(X)
Returns the integer value that is less than or equal to number x.

Ex:
select floor(2.83), floor(2.49), floor(-1.6) from dual;
4. ROUND(X)
Returns the round off value of the number X up to the number Y decimal places.

Ex:
select round(125.456,1), round(125.456,0), round(114.456,-1) from dual;
o/p: 125.4 125

5. TRUNC(X, Y)
Returns the value truncate of number X up to Y decimal places.

Ex:
select trunc(140.234,2), trunc(-54,1), trunc(5.7), trunc(42,-1) from dual;

2. Character (Or) Text Functions


These are the functions that accept character input and can return both character and number
values. Character functions are

1. LOWER
2. UPEER
3. INITCAP
4. LTRIM
5. RTRIM
6. TRIM
7. SUBSTR(STR, M, N)
8. LENGTH
9. LPAD
10. RPAD

1. LOWER(str)
All the letters in string_value is converted to lower case.

Ex:
select lower('GOOD Morning') from dual;
o/p: good morning

2. UPPER(str)
Returns all letter in 'string_value' is converted to upper case.

Ex:
select upper('Good morning') from dual;
o/p: GOOD MORNING

3. INITCAP(str)
Returns all the letter in 'string_value' is converted to mixed case.

Ex:
select initcap('good morning') from dual;
o/p: Good Morning

4. LTRIM(str, trim_text)
Returns all occurrences of trim_text is removed from left of string.
Ex:
select ltrim('Good Morning', 'Good') from dual;
o/p: Morning

5. RTRIM(str, trim_text)
Returns all occurrences of trim_text is removed from right of string.

Ex:
select rtrim('Good Morning', 'Morning') from dual;
o/p: Good

6. LENGTH(str)
Returns number of characters in 'string_value'

Ex:
select length('Good Morning') from dual;
o/p: 12

7. LPAD(str, n, pad_value)
Padded with 'pad_value'. The length of the whole string will be of 'n' characters.

Ex:
Select lpad('Good', 6, '*') from dual;
o/p: **Good

8. RPAD(str, n, pad_value)
Returns 'string_value' right padded with 'pad_value' . The length of the whole string will be of
'n' characters.

Ex:
select rpad('Good', 6, '*') from dual;
o/p: Good**

Date Functions
These are functions that take values that are of data type date as input and return values of data
type date, except for the MONTHS_BETWEEN functions which return a number as output.

1. add_months(date, n)
2. month_between(x1, x2)
3. next_day(x, weak_day)
4. last_day(x)
5. sysdate
6. systimestamp

1. add_months(date, n)
Returns a value after adding 'n' months to the data 'x'

Ex:
select add_months('25-DEC-16', 3) from dual;
o/p: 25-MAR-17
2. months_between(x1, x2)
Returns the number of months between dates x1 and x2

Ex:
select months_between('25-MAR-17', '25-DEC-17') from dual;
o/p: 3

3. next_day(x, week_day)
Returns the next date of the 'week_day' on or after the date 'x' occures.

Ex:
select next_day('25-DEC-16', 'Sunday') from dual;
o/p: 01-JAN-17

4. last_day(x)
It is used to determine the number of days remaining in a month from the date 'x' specified.

Ex:
select last_day('25-DEC-16') from dual;

5. sysdate
Returns the system current date and time.

Ex:
select sysdate from dual;
o/p: 03-JAN-17

6. systimestamp

The TIMESTAMP datatype is an extension of the DATE datatype. It stores year, month, day, hour,
minute, and second values. It also stores fractional seconds, which are not stored by the DATE
datatype.

SQL> SELECT SYSTIMESTAMP FROM DUAL;


SYSTIMESTAMP
---------------------------------------------------------------------------
03-JAN-15 08.33.45.806000 AM +05:30

Conversion Functions
These are the functions that help us to convert a value in one form to another form. The functions
are

1. to_char(x, [y])
2. to_date(x, [date format] )
3. nvl(x,y)

1. to_char(x, [y])
Converts numeric and date values to character and string values. It cannot be used for
calculations since it is a string value.

Ex:
select to_char(3000,'$9999') from dual;
o/p: $9999

2. to_date(x, [date format] )


Converts a valid numeric and character value to a date value date is formatted to the format
specified by the 'date_format'

Ex:
select to_date('01-JAN-08') from dual;
o/p: 01-JAN-08

3. nvl(x, y)
If 'x' is NULL, replace it with 'y'. 'x' and 'y' must be the same data type.

Ex:
select nvl(null,1) from dual;
o/p: 1

You might also like