Date Conversions
Date Conversions
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 element to remove padded blanks or suppress leading zeros Is separated from the date value by a comma
Displaying a Date in a Specific Format Previously, all Oracle date values were displayed in the DD-MON-YY format. The TO_CHAR function allows you to convert a date from this default fomat to one specified by you. Guidelines The format model must be enclosed in single quotation marks and is case sensitve; The format model can include any valid date format element. Be sure to separate the date value from the format model by a comma. The names of days and months in the output are automatically padded with blanks. To remove padded blanks or to suppress leading zeros, use the fill mode fm element. Yon can resize the display width of the resulting character feld with the SQL*Plus COLUMN command. The resultant column width is 80 characters by default.
Examples
SELECT empno, TO_CHAR(hiredate, 'MM/YY') "E GR TARH" FROM emp WHERE ename = 'BLAKE' ;
EMP O 7698 E AME BLAKE E GR 05/81
SELECT ename, empno TO_CHAR( hiredate , 'fmMonth DD, YYYY' ) Hiredate FROM emp WHERE deptno = 30
E AME ALLEN WARD MARTIN BLAKE TURNER JAMES 6 rows selected. EMP O HIREDATE 7499 ubat 20, 1981 7521 ubat 22, 1981 7654 Eyll 28, 1981 7698 Mays 1, 1981 7844 Eyll 8, 1981 7900 Aralk 3, 1981
Example SELECT sysdate, TO_CHAR ( sysdate, 'fmDD.MM.YYYY fmHH:MI:SS PM' ) Zaman FROM dual ;
SYSDATE 18/03/2007 ZAMA 18.3.2007 06:23:02 S
SELECT sysdate, TO_CHAR ( sysdate, 'fmDD.MM.YYYY fmHH:MI:SS AM' ) Zaman FROM dual ;
SYSDATE 18/03/2007 ZAMA 18.3.2007 06:23:02 S
SELECT sysdate, TO_CHAR ( sysdate, 'fmDD.MM.YYYY fmHH12:MI:SS PM' ) Zaman FROM dual ;
ZAMA 18.3.2007 06:23:02 S
SYSDATE 18/03/2007
SELECT sysdate, TO_CHAR ( sysdate, 'fmDD.MM.YYYY fmHH24:MI:SS AM' ) Zaman FROM dual ;
SYSDATE 18/03/2007 ZAMA 18.3.2007 18: 23:02 S
SELECT ename, TO_CHAR(hiredate, 'fmDdspth "of" Month YYYY fmHH:MI:SS PM' ) HIREDATE FROM emp;
14 rows selected.
Examples : YY , CC , SCC
SELECT TO_CHAR( TO_DATE('28-11-1942') , 'YY') Yzyl FROM dual; YIL
42
Examples : D , DD , DDD
SELECT TO_CHAR( TO_DATE('28-11-1942') , 'fmd' ) "Day Of Week" FROM dual ; Day Of Week 6
SELECT TO_CHAR( TO_DATE('28-11-1942') , 'fmdd' ) "Day Of Month" FROM dual ; Day Of Month 28
SELECT TO_CHAR( TO_DATE('28-11-2942') , 'fmddd' ) "Day Of Year" FROM dual ; Day of Year 332
The next example outputs the Julian Day; the number of days since 31 December 4713 BC of the given date. SELECT TO_CHAR( TO_DATE('28-11-2942') , 'fmJ' ) "Julian Day" FROM dual ; Julian Day
2430692
SELECT TO_CHAR( TO_DATE('18-09-1972') , 'w' ) "Week of Month" FROM dual ; Week of Month 3
SELECT TO_CHAR( TO_DATE('18-09-1972') , 'ww' ) "Week of Year" FROM dual ; Week of Month 38
10
TO_CHAR (datetime)
TO_CHAR (datetime) converts date of DATE, TIMESTAMP, TIMESTAMP WITH TIME ZONE, or TIMESTAMP WITH LOCAL TIME ZONE datatype to a value of VARCHAR2 datatype in the format specified by the date format fmt. If you omit fmt, then date is converted to a VARCHAR2 value as follows:
DATE is converted to a value in the default date format. TIMESTAMP and TIMESTAMP WITH LOCAL TIME ZONE are converted to values in the default timestamp format. TIMESTAMP WITH TIME ZONE is converted to a value in the default timestamp with time zone format.
The 'nlsparams' specifies the language in which month and day names and abbreviations are returned. This argument can have this form: 'NLS_DATE_LANGUAGE = language'
CREATE TABLE date_tab ( ts_col TIMESTAMP, tsltz_col TIMESTAMP WITH LOCAL TIME ZONE, tstz_col TIMESTAMP WITH TIME ZONE);
11
12
TO_DATE Function
Converts a character string to a date format.
TO_DATE (char[,
'fmt'])
TO_NUMBER and TO_DATE Functions You may want to convert a character string to either a number or a date. To accomplish this task, you use the TO_NUMBER or TO_DATE functions. The format model you choose will be based on the previously demonstrated format elements. Example Display the names and hire dates of all the employees who joined on February 22. 1981. SELECT ename, hiredate FROM emp WHERE hiredate = TO_DATE ( 'ubat 22, 1981', 'Month dd, YYYY');
E AME WARD 22/02/1981 HIREDATE
13
Time Formats
Description Meridian indicator Meridian indicator with periods Hour of day or hour (1 - 12) or hour (0-23) Minute (0-59) Second (0-59) Seconds past midnight (0-86399)
14
TARIH 01/01/2099
'yyyy/dd/mm'
TO_DATE('2 07/09/2003
to_date (string1, [format_mask], [nls_language]) to_date ('2003/07/09', 'yyyy/mm/dd'); /*July 9, 2003*/ to_date ('070903', 'MMDDYY'); /*July 9, 2003*/ to_date ('20020315', 'yyyymmdd'); /*Mar 15, 2002*/
16
17
18
SELECT TO_DATE('Aralk 15, 98' , 'Month DD, YY') Tarih FROM dual ; TARH 15/12/2098
SELECT TO_DATE('Aralk 15, 98' , 'Month DD, RR') Tarih FROM dual ; TARH 15/12/1998
SELECT TO_DATE('Aralk 15, 07' , 'Month DD, RR') Tarih FROM dual ; TARH 15/12/2007
19
RR Date Format
Current Year 1995 1995 2001 2001 Specified Date 27-OCT-95 27-OCT-17 27-OCT-17 27-OCT-95 RR Format 1995 2017 2017 1995 YY Format 1995 1917 2017 2095
If the specified two-digit year is: 0-49 If two digits of the current year are: 5 3-49 The return date is in the current century 50-99 The return date is in the century before the current ne The return date is in the current century
The RR date format is similar to the YY element, but it allows you to specify differeut centuries You can use the RR date format element instead of YY, so that the century of the return value according to the specified two-digits of the current year. The table on the slide summarizes the behavior of the RR element.
20
SYSDATE = CURRENT_DATE
and DUAL
SYSDATE is a date function that returns the current date and time. It is customary to select SYSDATE from a dummy table called DUAL. select to_char(sysdate, 'DD-Mon-YYYY HH24:MI:SS') as "Current Time" from dual;
Current Time 18-Mar-2007 17:37:10
21
Example
Display the date of the next Friday that is six months from the hiredate. The resultant date should appearas Friday, March 12th 1982. Order the results by hiredate.
SELECT TO_CHAR (NEXT_DAY (ADD_MONTHS (hiredate, 6), 'CUMA') , 'fmDay, Month ddth, YYYY' ) "Next 6 Months Review" FROM emp ORDER BY hiredate ;
ext 6 Months Review Cuma, Haziran 19th, 1981 Cuma, Austos 21st, 1981 Cuma, Austos 28th, 1981 Cuma, Ekim 9th, 1981 Cuma, Kasm 6th, 1981 Cuma, Aralk 11th, 1981 Cuma, Mart 12th, 1982 Cuma, Nisan 2nd, 1982 Cuma, Mays 21st, 1982 Cuma, Haziran 4th, 1982 Cuma, Haziran 4th, 1982 Cuma, Temmuz 30th, 1982 Cuma, Haziran 10th, 1983 Cuma, Temmuz 15th, 1983 14 rows selected.
22
Practice 3
1. 2. Write a query to display the current date. Label the column Date. 2 Display the employee number, name, salary, and salary increase by 15% expressed as a whole number. Label the column New Salary. Save your SQL statement to a file named p3q2.sql. Run your query in the file p3q2.sql.
3.
23
Practice 3 (continued) 7. Write a query that produces the following for each employee. <employee name> eams <salary> montly but wants <3 times salary >. Label the column Dream Salaries.
If yon have time, complele the following exercises: 8. Create a query to display name and salary for all employees. Format the salary to be 15 characters long, left-padded with $. Label the column SALARY.
Practice 3 (continued) If you want extra challenge, complete the following exercises: 11. Create a query that will display the employee name and commission amount. If the does not earn commission, put "No Commission". Label the column COMM.
12.Create a query that displays the employees names and indicates the amounts of their salaries through asterisks. Each asterisk signifies a hundred dollars. Sort the data in descending order of salary. Label the column EMPLOYEE_AND_THEIR_SALARIES.
24