Chapter 03-Using Single-Row Functions to Customize Output(03)
Working with Dates
- The Oracle database stores dates in an internal numeric format:century,year,month,day,hours,minutes,and seconds.
- The default date display format is DD-MON-RR.
- -Enables you to store 21st-century dates in the 20th century by specifying only the last two digits of the year.
- -Enables you to store 20th-century dates in the 21st century in the same way.
View Code
SQL> SELECT last_name,hire_date FROM employees WHERE hire_date < '01-FEB-03'; LAST_NAME HIRE_DATE ------------------------- --------- Mavris 07-JUN-02 Baer 07-JUN-02 Higgins 07-JUN-02 Gietz 07-JUN-02 De Haan 13-JAN-01 Greenberg 17-AUG-02 Faviet 16-AUG-02 Raphaely 07-DEC-02 8 rows selected.
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 |
If the specified two-digit year is: | |||
0-49 | 50-99 | ||
If two digits of the current year are: | 0-49 | The return date is in the current centry | The return date is in the century before the current one. |
50-99 | The return date is in the century after the current one | The return date is in the current century |
Using the SYSDATE Function
SYSDATE is a function that returns:
- Date
- Time
View Code
SQL> SELECT TO_CHAR(sysdate,'YYYY-MM-DD HH24:MI:SS') CURRENTDATE FROM dual; CURRENTDATE ------------------- 2013-04-12 13:05:31
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.
Using Arithmetic Operators with Dates
View Code
SQL> SELECT last_name,(SYSDATE-hire_date)/7 AS WEEKS FROM employees WHERE department_id = 90; LAST_NAME WEEKS ------------------------- ---------- King 512.508074 Kochhar 394.365217 De Haan 638.936645
Date-Mainpulation Functions
Function | Result |
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 |
Using Date Functions And Using ROUND and TRUNC Functions with Dates.
View Code
SQL> SELECT MONTHS_BETWEEN('01-SEP-95','11-JAN-94') FROM DUAL; MONTHS_BETWEEN('01-SEP-95','11-JAN-94') --------------------------------------- 19.6774194 SQL> SELECT ADD_MONTHS('31-JAN-96',1) FROM DUAL; ADD_MONTH --------- 29-FEB-96 SQL> SELECT NEXT_DAY('01-SEP-95','FRIDAY') FROM DUAL; NEXT_DAY( --------- 08-SEP-95 SQL> SELECT LAST_DAY('01-FEB-95') FROM DUAL; LAST_DAY( --------- 28-FEB-95 SQL>
View Code
SQL> SELECT SYSDATE FROM DUAL; SYSDATE --------- 12-APR-13 SQL> SELECT ROUND(SYSDATE,'MONTH') FROM DUAL; ROUND(SYS --------- 01-APR-13 SQL> SELECT ROUND(SYSDATE,'YEAR') FROM DUAL; ROUND(SYS --------- 01-JAN-13 SQL> SELECT TRUNC(SYSDATE,'MONTH') FROM DUAL; TRUNC(SYS --------- 01-APR-13 SQL> SELECT TRUNC(SYSDATE,'YEAR') FROM DUAL; TRUNC(SYS --------- 01-JAN-13 SQL>
Summary
In this lesson,you should have learned how to:
- Perform calulations on data using functions.
- Modify individual data items using funcions.