SQL_Fns
SQL_Fns
The Oracle implementation of SQL provides a number of functions that can be used in SELECT statements.
Functions are typically grouped into the following:
Single row functions - Operate on column values for each row returned by a query.
Group functions - Operate on a collection (group) of rows (already discussed).
The following is an overview and brief description of single row functions. x is some number, s is a string of
characters and c is a single character. For illustration we use DUAL pseudo column but it may be applied on
any appropriate column or expression.
Illustration:
SELECT ABS(-15) "Absolute" FROM DUAL; SELECT CEIL(15.7) "Ceiling" FROM DUAL;
Absolute Ceiling
---------- ----------
15 16
SELECT FLOOR(15.7) "Floor" SELECT MOD(11,4) "Modulus"
FROM DUAL; FROM DUAL;
Floor Modulus
---------- ----------
15 3
SELECT POWER(3,2) "Raised" SELECT ROUND(15.193,1) "Round"
FROM DUAL FROM DUAL;
Raised
---------- Round
9 ----------
15.2
SELECT ROUND(15.193,-1) "Round" SELECT TRUNC(15.79,1) "Truncate"
FROM DUAL FROM DUAL
Round Truncate
---------- ----------
20 15.7
SELECT TRUNC(15.79,-1) "Truncate"
FROM DUAL
Truncate
----------
10
1
Compiled by Manas Ghosh
Character functions include:
C h a r a c t e r f u n c t i o n s t h a t r e t u r n n u mb e r s i n c l u d e :
Illustration:
Dog
---
CAT
Job
-----------------
RAJA SEN is a CLERK
Lowercase
--------------------
mr. raja sen
LPAD example
---------------
*.*.*.*.*Page 1
Subs
----
CDEF
2
Compiled by Manas Ghosh
SELECT SUBSTR('ABCDEFG',-5,4) "Subs" FROM DUAL
Subs
----
CDEF
Length in characters
--------------------
7
TO_CHAR (date, format) Converts a date column to a string of characters. format is a set
of Date formatting codes where:
Illustration:
TO_CHAR(SYSDATE,'"
------------------
Today is Friday
TO_CHAR
--------
3
Compiled by Manas Ghosh
25/03/05
TO_CHAR(SY
----------
25-03-2005
TO_CHAR(T
---------
Tuesday
TO_DATE can also accept numbers without single quotient mark provided they are
formatted properly.
TO_DATE(1
---------
05-NOV-95
Query: List date of join of each employee in ‘DD/MM/YYYY’ format with code, name.
Returns the date d plus n months. The argument n can be any integer. If d is
the last day of the month or if the resulting month has fewer days than the day
ADD_MONTHS(d,n)
component of d, then the result is the last day of the resulting month.
Otherwise, the result has the same day component as d.
Returns the date of the last day of the month that contains d. You might use
LAST_DAY(d)
this function to determine how many days are left in the current month.
Returns number of months between dates d1 and d2. If d1 is later than d2,
MONTHS_BETWEEN(d1,d2)
result is positive; if earlier, negative. If d1 and d2 are either the same days of
4
Compiled by Manas Ghosh
the month or both last days of months, the result is always an integer;
otherwise ORACLE calculates the fractional portion of the result based on a
31-day month and also considers the difference in time components of d1 and
d2.
Returns the date of the first weekday named by char that is later than the date
d. The argument char must be a day of the week in your session's date
NEXT_DAY(d,char)
language. The return value has the same hours, minutes, and seconds
component as the argument d.
Returns the current date and time. Requires no arguments. In distributed SQL
SYSDATE statements, this function returns the date and time of your local database.
You cannot use this function in the condition of a CHECK constraint.
The SYSDATE pseudo-column shows the current system date and time. Adding 1 to SYSDATE
will advance the date by 1 day. Use fractions to add hours, minutes or seconds to the date. Look
at these examples:
select sysdate, sysdate+1/24, sysdate +1/1440, sysdate +
1/86400 from dual;
NOW NOW_PLUS_30_SECS
-------------------- --------------------
03-JUL-2002 16:47:23 03-JUL-2002 16:47:53
5
Compiled by Manas Ghosh
Wednesday or Friday at 9 ),NEXT_DAY(sysdate,''WEDNESDAY''), NEXT_DAY(sysdate,''FRIDAY'' ))) +
a.m (9/24)
Illustration:
Next Month
-----------
17-JAN-1981
SELECT MONTHS_BETWEEN(TO_DATE('02-02-1995','MM-DD-YYYY'),
TO_DATE('01-01-1995','MM-DD-YYYY') ) "Months" FROM DUAL;
Months
----------
1.03225806
NEXT DAY
---------
17-MAR-92
NOW
--------------------
10-29-1993 20:27:11.
The following statement selects the dates that each employee from department 20 was hired and uses the
TO_CHAR function to convert these dates to character strings with the format specified by the date format
model 'fmMonth DD, YYYY':
With this format model, Oracle returns the hire dates with the month spelled out, two digits for the day, and
the century included in the year.
6
Compiled by Manas Ghosh
Text, dates and numbers can be combined using the various conversion functions. In the following example,
the TO_CHAR function is used to convert the date column ‘dob’ into a character string.
SENTENCE
----------------------------------------
The oldest employee was born on 10/11/1927
Date math results in a numerical answer that must be converted to characters to concatenate with other
character strings as in this next example:
SENTENCE
-----------------------------------------------------------------
The oldest employee was born on 10/11/1927 and is now 70 years old.
In the previous examples of SQL statements, the default format of data of type DATE has been in the form:
DD-MON-YY
The TO_CHAR and TO_DATE functions can be used to convert dates to other formats, however, this may
become inconvenient, especially when inserting a large number of rows.
The ALTER SESSION statement can be used to alter various characteristics of the current SQL*Plus
session including the default date format. This statement is often used to format dates to conform to regional
customs. The syntax of ALTER SESSION for use with changing the default date format is as follows:
For example, to change the default date to include a full four digit year, issue the following ALTER SESSION
statement:
From this point, all INSERT, UPDATE and DELETE statements must format the date accordingly. Also, any
SELECT statements will return the date formatted accordingly.
7
Compiled by Manas Ghosh
Note that this change only remains in effect for the current session. Logging out of SQL*Plus and logging
back in (or re-connecting to the Oracle database using the connect command) will reset the date format back
to its default.
The RR date format element is similar to the YY date format element, but it provides additional flexibility for
storing date values in other centuries. The RR date format element allows you to store twenty-first century
dates in the twentieth century by specifying only the last two digits of the year. It will also allow you to store
twentieth century dates in the twenty-first century in the same way if necessary.
If you use the TO_DATE function with the YY date format element, the date value returned is always in the
current century. If you use the RR date format element instead, the century of the return value varies
according to the specified two-digit year and the last two digits of the current year. The following table
summarizes the behavior of the RR date format element.
The following example demonstrates the behavior of the RR date format element.
Illustration:
4-digit year
------------
1995
4-digit year
------------
2017
4-digit year
------------
1995
8
Compiled by Manas Ghosh
4-digit year
------------
2017
Note that the queries return the same values regardless of whether they are issued before or after the year
2000. The RR date format element allows you to write SQL statements that will return the same values after
the turn of the century.
S o me a d d i t i o n a l i m p o r t a n t f u n c t i o n s a r e :
DECODE (s, search1, result1, Compares s with search1, search2, etc. and returns the
search2, result2) corresponding result when there is a match.
NVL (s, expression) If s is NULL, return expression. If s is not null, then return s.
USER Returns the username of the current user.
For example: SELECT USER FROM DUAL;
Suppose, in EMP table, there is a column SEX of type VARCHAR2(1). It contains either ’ M’ for male
employee or ‘F’ for female employee. Now it is needed to return "MALE" if the value is "M" ; "FEMALE" if the
value is "F" ; "UNKNOWN" if the field is neither M nor F. The query to do this will be
ENAME COMMISSION
--------- -----------
BAKER 300
MAY 500
JOLLEY 1400
KOEHL NOT APPLICABLE
LAZARRO 0
FREER NOT APPLICABLE
You can convert numeric values to characters by using the TO_CHAR() function as shown in the
following examples:
9
Compiled by Manas Ghosh
How to Convert Characters to Numbers?
You can convert characters to numbers by using the TO_NUMBER() function as shown in the
following examples:
You can convert dates to characters using the TO_CHAR() function as shown in the following
examples:
07-MAY-2006
2006/05/07
May 7, 2006
You can convert dates to characters using the TO_DATE() function as shown in the following
examples:
07-MAY-06
07-MAY-06
07-MAY-06
10
Compiled by Manas Ghosh
SELECT TO_DATE('May 7, 2006', 'fmMONTH DD, YYYY') FROM DUAL;
07-MAY-06
You can convert dates to characters using the TO_CHAR() function as shown in the following
examples:
04:49:49
16:52:57.847000000
69520
11
Compiled by Manas Ghosh