0% found this document useful (0 votes)
3 views13 pages

Lab 8 Functions

The document outlines the structure and data entries for an Employee table in an Oracle database, including fields such as employee number, name, job title, manager, hire date, salary, commission, and department number. It also describes various date functions and formatting options available in Oracle SQL, including arithmetic with dates, explicit data type conversions, and the use of the TO_CHAR, TO_NUMBER, and TO_DATE functions. Additionally, it explains the RR date format for handling years in date queries.

Uploaded by

Purbasha Roy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views13 pages

Lab 8 Functions

The document outlines the structure and data entries for an Employee table in an Oracle database, including fields such as employee number, name, job title, manager, hire date, salary, commission, and department number. It also describes various date functions and formatting options available in Oracle SQL, including arithmetic with dates, explicit data type conversions, and the use of the TO_CHAR, TO_NUMBER, and TO_DATE functions. Additionally, it explains the RR date format for handling years in date queries.

Uploaded by

Purbasha Roy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

Functions

Employee Table
• CREATE TABLE EMP
• (EMPNO NUMBER(4) NOT NULL,
• ENAME VARCHAR2(10),
• JOB VARCHAR2(9),
• MGR NUMBER(4),
• HIREDATE DATE,
• SAL NUMBER(7, 2),
• COMM NUMBER(7, 2),
• DEPTNO NUMBER(2));

• INSERT INTO EMP VALUES


• (7369, 'SMITH', 'CLERK', 7902,
• TO_DATE('17-DEC-1980', 'DD-MON-YYYY'), 800, NULL, 20);
• INSERT INTO EMP VALUES
• (7499, 'ALLEN', 'SALESMAN', 7698,
• TO_DATE('20-FEB-1981', 'DD-MON-YYYY'), 1600, 300, 30);
• INSERT INTO EMP VALUES
• (7521, 'WARD', 'SALESMAN', 7698,
• TO_DATE('22-FEB-1981', 'DD-MON-YYYY'), 1250, 500, 30);
Employee Table
• INSERT INTO EMP VALUES
• (7566, 'JONES', 'MANAGER', 7839,
• TO_DATE('2-APR-1981', 'DD-MON-YYYY'), 2975, NULL, 20);
• INSERT INTO EMP VALUES
• (7654, 'MARTIN', 'SALESMAN', 7698,
• TO_DATE('28-SEP-1981', 'DD-MON-YYYY'), 1250, 1400, 30);
• INSERT INTO EMP VALUES
• (7698, 'BLAKE', 'MANAGER', 7839,
• TO_DATE('1-MAY-1981', 'DD-MON-YYYY'), 2850, NULL, 30);
• INSERT INTO EMP VALUES
• (7782, 'CLARK', 'MANAGER', 7839,
• TO_DATE('9-JUN-1981', 'DD-MON-YYYY'), 2450, NULL, 10);
• INSERT INTO EMP VALUES
• (7788, 'SCOTT', 'ANALYST', 7566,
• TO_DATE('09-DEC-1982', 'DD-MON-YYYY'), 3000, NULL, 20);
• INSERT INTO EMP VALUES
• (7839, 'KING', 'PRESIDENT', NULL,
• TO_DATE('17-NOV-1981', 'DD-MON-YYYY'), 5000, NULL, 10);
Employee Table
• INSERT INTO EMP VALUES
• (7844, 'TURNER', 'SALESMAN', 7698,
• TO_DATE('8-SEP-1981', 'DD-MON-YYYY'), 1500, NULL, 30);
• INSERT INTO EMP VALUES
• (7876, 'ADAMS', 'CLERK', 7788,
• TO_DATE('12-JAN-1983', 'DD-MON-YYYY'), 1100, NULL, 20);
• INSERT INTO EMP VALUES
• (7900, 'JAMES', 'CLERK', 7698,
• TO_DATE('3-DEC-1981', 'DD-MON-YYYY'), 950, NULL, 30);
• INSERT INTO EMP VALUES
• (7902, 'FORD', 'ANALYST', 7566,
• TO_DATE('3-DEC-1981', 'DD-MON-YYYY'), 3000, NULL, 20);
• INSERT INTO EMP VALUES
• (7934, 'MILLER', 'CLERK', 7782,
• TO_DATE('23-JAN-1982', 'DD-MON-YYYY'), 1300, NULL, 10);
Dates
• Oracle database stores dates in an internal numeric format: century, year,
month, day, hours, minutes, seconds
• The default date display format is DD-MON-RR
SELECT ENAME, HIREDATE FROM EMP WHERE ENAME like 'B%';

SYSDATE is a function that returns:


• Date
• Time
Arithmetic with Dates
• Add or subtract a number to or from a date for a resultant date value.
• Subtract two dates to find the number of days between those dates.
• Add hours to a date by dividing the number of hours by 24.

SELECT ENAME, (SYSDATE-HIREDATE)/7 AS WEEKS FROM EMP WHERE DEPTNO = 20;


Date Functions
• MONTHS_BETWEEN -> Number of months between two dates
• ADD_MONTHS -> Add calendar months to date
• NEXT_DAY -> Next day of the date specified
• LAST_DAY -> Last day of the month
• ROUND -> Round date
• TRUNC -> Truncate date

Assume SYSDATE = '25-JUL-95':

• MONTHS_BETWEEN ('01-SEP-95','11-JAN-94') • ROUND(SYSDATE,'MONTH')


• 19.6774194 • 01-AUG-95
• ADD_MONTHS ('11-JAN-94',6) • ROUND(SYSDATE ,'YEAR')
• '11-JUL-94' • 01-JAN-96
• NEXT_DAY ('01-SEP-95','FRIDAY') • TRUNC(SYSDATE ,'MONTH')
• '08-SEP-95' • 01-JUL-95
• LAST_DAY('01-FEB-95') • TRUNC(SYSDATE ,'YEAR')
• '28-FEB-95' • 01-JAN-95
Explicit Data Type Conversion

CHARACTER TO_NUMBER NUMBER TO_CHAR CHARACTER

CHARACTER TO_DATE DATE TO_CHAR CHARACTER


TO_CHAR Function with Dates

• TO_CHAR(date, 'format_model')
The format model:
• Must be enclosed in single quotation marks and is case sensitive
• Can include any valid date format element
• Is separated from the date value by a comma

Elements of the Date Format Model


YYYY Full year in numbers • Time elements format the time portion of the date.
YEAR Year spelled out • HH24:MI:SS AM 15:45:32 PM
MM Two-digit value for month • Add character strings by enclosing them in double quotation
MONTH Full name of the month marks.
MON Three-letter abbreviation of the month • DD "of" MONTH 12 of OCTOBER
DY Three-letter abbreviation of the day of the week • Number suffixes spell out numbers.
DAY Full name of the day of the week • Ddspth fourteenth
DD Numeric day of the month
TO_CHAR Function with Dates
• SELECT ENAME, TO_CHAR(HIREDATE, 'DD Month YYYY') AS HIRE_DATE FROM
EMP;
TO_CHAR Function with Numbers

• TO_CHAR (number, 'format_model ')


These are some of the format elements you can use with the TO_CHAR function to display a number
value as a character
9 -> Represents a number
0 -> Forces a zero to be displayed
$ -> Places a floating dollar sign
L -> Uses the floating local currency symbol
. -> Prints a decimal point
, -> Prints a thousand indicator

SELECT TO_CHAR(SAL, '$99,999.00') SALary FROM EMP WHERE ENAME = 'WARD';


TO_NUMBER and TO_DATE Functions
• Convert a character string to a number format using the TO_NUMBER function:
• TO_NUMBER (char , 'format_model')

• SELECT TO_NUMBER('1210.73', '9999.99') FROM DUAL;

Convert a character string to a date format using the TO_DATE function:


TO_DATE(char, 'format_model ')

SELECT TO_DATE('January 15, 1989, 11:00 A.M.', 'Month dd, YYYY, HH:MI
A.M.') FROM DUAL;
These functions have an fx modifier. This modifier specifies the exact matching for the character
argument and date format model
RR Date Format
Current Year Specified Date RR Format YY Format
1995 27-OCT-95 1995 1995
1995 27-OCT-17 2017 1917
2001 27-OCT-17 2017 2017
2001 27-OCT-95 1995 2095

• To find employees hired prior to


1990, use the RR format, which
produces the same results whether
the command is run in 1999 or
now:

SELECT ENAME, TO_CHAR(HIREDATE, 'DD-Mon-YYYY')


FROM EMP WHERE HIREDATE < TO_DATE('01-Jan-81', 'DD-
Mon-RR');

You might also like