5 Functions

Oracle comes with many built-in functions. Here are some of the more popular functions:
 

 5.1 Numeric, Character, and Date Functions


5.2 Group Functions

Group functions include:
 


5.3 Functions for Conversions

The functions that allow you to use dates will probably be useful to you. As mentioned earlier, there really isn't such a thing as a date literal. Instead, you have to use a character string or VARCHAR2 and then use a function to convert it. Functions also exist that convert characters to numbers and vice-versa.

All 4 of these functions are listed below. d, char, and n is what you're converting from, and fmt is the optional format. For dates, leaving out fmt means that the system default date format is used (the default date format is DD-MON-YY e.g. 15-MAY-01).  The Oracle date format actually includes both a date and a time component.  If you do not specify a time component (you'll see later how to specify times), then the time component is set to 12:00 A.M.  If you only specify a time component, then the date component is set to the current date.

For the conversion from numbers to characters, the output will just be a VARCHAR2 big enough to hold the numbers if you leave out fmt.
 


Although Oracle automatically converts character values that are in the default date format to date values, it is best to be explicit and use the TO_DATE function wherever a date value is expected.
 

For NUMBERs, here are some format elements (fmt):
 


Note:  There is no need to specify a negative sign because it is automatically placed in front of a number if the number is negative.
 

For DATEs, here are some of the format elements (fmt):
 

 

5.4  Date Examples


EX 1.

SELECT SYSDATE
FROM DUAL;

Returns

SYSDATE
-------------
15-MAY-01

This returns the current date in the default date format.  Dual is just a dummy table.


EX 2.

SELECT TO_CHAR(SYSDATE, 'Dy fmMonth DD HH:MI:SS') AS "Current Date and Time"
FROM DUAL;

Returns

Current Date and Time
---------------------
Thu May 17 3:39:22

This example shows how to use TO_CHAR() to control the output format of the current date.  The fm in the format string is a format modifier that removes blank padding and leading zeros in the return value of TO_CHAR.  For dates, right padding in character elements such as MONTH and leading zeros of number elements such as HH are removed.  For example, without the fm in the above statement the output would be

Current Date and Time
-------------------------
Thu May       17 03:39:22

The scope of fm is the rest of the format string following the fm.  Each subsequent occurence of fm toggles its effect.

Capitalization in the format string can affect the output of the date.  For example, if the above format string was
'DY fmMONTH DD HH:MI:SS', the output would be

Current Date and Time
------------------------
THU MAY 17 3:39:22


EX 3.

INSERT INTO calender
VALUES (TO_DATE('2001/05/15', 'YYYY/MM/DD'));

This inserts the given date into the table named calender.  This does not store 15-MAY-01 as 2001/05/15 in the calender table.  For example, typing

SELECT * FROM calender

Returns

The Date
-------------
15-MAY-01


EX 4.

SELECT (SYSDATE - 1) AS "Yesterday"
FROM DUAL;

Returns

Yesterday
---------
16-MAY-01

You can add and subtract constants to and from dates.  The constants are treated as days.  To add months to a date, you can use the ADD_MONTHS function.  You can also subtract dates to and from each other.  This returns the number of days between the dates.  To find the number of months between dates, you can use the MONTHS_BETWEEN function.  The standard comparison operators such as <, =, <>, != (<> is the same as !=), <=, etc. can be used with dates.

 

5.5  Number Example


EX 1.

SELECT TO_CHAR(999.0258, 'fm$9,990.99') AS Salary
FROM DUAL;

Returns

SALARY
----------
$999.03

The fm in the format string is a format modifier for TO_CHAR() that removes blanks to the left of the number so that the number appears left aligned.  It is ok for the format string to be longer than the actual number at the left or right of the decimal point.  However, if the format string is shorter than the number at the left of the decimal point, a string of '###...' would be returned.


For more information on Oracle's built-in functions, refer to the "Functions" chapter in the Oracle SQL Reference.  For more information on date and number format elements, refer to the "Basic Elements of Oracle SQL" chapter in the same reference.