Oracle Lab 5
Oracle Lab 5
Function Description
Number of months between two
MONTHS_BETWEEN
dates
• MONTHS_BETWEEN(’01-SEP-95’,’11-JAN-94’) 19.6774194
• ADD_MONTHS(’11-JAN-94’,6) ’11-JUL-94’
• NEXT_DAY(’01-SEP-95’,’FRIDAY’) ‘08-SEP-95’
• LAST_DAY(’01-SEP-95’) ’30-SEP-95’
Using Date Functions
• ROUND(’25-JUL-95’,’MONTH’) 01-AUG-95
• ROUND(’25-JUL-95’,’YEAR’) 01-JAN-96
• TRUNC(’25-JUL-95’,’MONTH’) 01-JUL-95
• TRUNC(’25-JUL-95’,’YEAR’) 01-JAN-95
Conversion Functions
Data type
Conversion
TO_CHAR TO_CHAR
TO_CHAR Function with dates
TO_CHAR(date, ‘fmt’)
The format model :
•Must be enclosed in single quotation marks and is case
sensitive.
• Can include any valid date format element.
• Has an fm (fill mode) element to remove padded blanks or
suppress leading zeros.
• Is separated from the date value by a comma.
Example:
SQL> SELECT first_name, email, TO_CHAR(HIRE_DATE,
‘fmMM/YY')AS Month_Hired FROM EMPLOYEES WHERE job_id =
'AD_VP';
Element of date format model
YYYY Full year in numbers
BC or AD BC/AD indicator
B.C or A.D BC/AD Indicator with periods
Q quarter of the year
MM Month, two digit value
Element Description
MONTH Name of the month padded with
blanks to length of nine
characters
MON Name of the month, three letter
abbreviation.
RM Roman numeral month
WW or W Week of year or month
DDD or DD or D Day of year / month/ week
DAY Name of the day padded with
blanks to length of nine
character
DY Name of the Day; three letter
abbreviation
Element Description
AM or PM Meridian indicator
A.M or P.M Meridian indicator with periods
HH / HH12 / HH24 Hour of day or hour (1-12) or hour
(0-23)
MI Minute ( 0 –59 )
SS Second ( 0 – 59 )
SSSSS Seconds past midnight ( 0 –
86399)
TH Ordinal Number ( DDTH for 4TH )
SP Spelled out number (DDSP for
FOUR)
SPTH or THSP Spelled-out ordinal number
(DDSPTH – FOURTH)
Using TO_CHAR Function with dates
Example:
SELECT first_name, TO_CHAR(hire_date,'fmDdspth "of" Month
YYYY fmHH : MI : SS AM') AS HIREDATE FROM employees;
Sample Output:
ENAME HIREDATE
----------- ------------------------------------
KING Seventeenth of November 1981 12:00:00 AM
BLAKE First of May 1981 12 : 00 :00 AM
…….