TO_CHAR Function with Dates



Similar documents
Conversion Functions

Date / Time Arithmetic with Oracle

USING CONVERSION FUNCTIONS

Producing Readable Output with SQL*Plus

Chapter 1. Writing Basic. SQL Statements

Single-Row Functions Schedule: Timing Topic

Appendix A Practices and Solutions

Retrieving Data Using the SQL SELECT Statement. Copyright 2006, Oracle. All rights reserved.

MONASH UNIVERSITY. Faculty of Information Technology

Displaying Data from Multiple Tables. Chapter 4

Displaying Data from Multiple Tables

Database Access from a Programming Language: Database Access from a Programming Language

Database Access from a Programming Language:

.NET Standard DateTime Format Strings

3.GETTING STARTED WITH ORACLE8i

Subqueries Chapter 6

Aggregating Data Using Group Functions

Advanced Tutorials. The Dark Side of the Transparent Moon

Oracle Database 12c: Introduction to SQL Ed 1.1

6. SINIF İNGİLİZCE DERSİ KURS KAZANIMLARI VE TESTLERİ

Programming with SQL

REPORT GENERATION USING SQL*PLUS COMMANDS

SQL> SELECT ename, job, sal Salary. 1.4.Will the SELECT statement execute successfully? True/False

Utility Software II lab 1 Jacek Wiślicki, jacenty@kis.p.lodz.pl original material by Hubert Kołodziejski

5. SINIF İNGİLİZCE DERSİ KURS KAZANIMLARI VE TESTLERİ

Using AD fields in Policy Patrol

Oracle Database: Introduction to SQL

Oracle Database: Introduction to SQL

7. SINIF İNGİLİZCE DERSİ KURS KAZANIMLARI VE TESTLERİ

SQL Introduction Chapter 7, sections 1 & 4. Introduction to SQL. Introduction to SQL. Introduction to SQL

Database Design. Marta Jakubowska-Sobczak IT/ADC based on slides prepared by Paula Figueiredo, IT/DB

Using Single-Row Functions to Customize Output. Copyright 2006, Oracle. All rights reserved.

STRING, CONVERSION, AND MISCELLANEOUS FUNCTIONS

VAKIFBANK GLOBAL ECONOMY WEEKLY

Notepads Import File Specification

8. SINIF İNGİLİZCE DERSİ KURS KAZANIMLARI VE TESTLERİ

9. SINIF İNGİLİZCE DERSİ KURS KAZANIMLARI VE TESTLERİ

5.1 Database Schema Schema Generation in SQL

12. SINIF İNGİLİZCE DERSİ KURS KONULARI VE TESTLERİ

Creating PL/SQL Blocks. Copyright 2007, Oracle. All rights reserved.

SQL*Plus User s Guide and Reference

Retrieval: Multiple Tables and Aggregation

An Oracle White Paper August Express Mode Loading with SQL*Loader in Oracle Database 12c

Mini User's Guide for SQL*Plus T. J. Teorey

Oracle Database: Introduction to SQL

Oracle SQL. Course Summary. Duration. Objectives

MyOra 3.0. User Guide. SQL Tool for Oracle. Jayam Systems, LLC

Key Functions in Oracle SQL

Oracle For Beginners Page : 1

Review your answers, feedback, and question scores below. An asterisk (*) indicates a correct answer.

ISQL Reference Manual

Oracle 12c New Features For Developers

MyOra 3.5. User Guide. SQL Tool for Oracle. Kris Murthy

Volume 8: January March 2014

Best Practices in SQL Programming. Madhivanan

Çanakkale Onsekiz Mart University School of Foreign Languages Prep Programme Course Description and Curriculum for Main Course Coursebook

RDBMS Using Oracle. Lecture Week 7 Introduction to Oracle 9i SQL Last Lecture. kamran.munir@gmail.com. Joining Tables

How to Design a Form Report (RTF) Output

Hal E-Bank Foreign payments (Format of export/import files)

1 Stored Procedures in PL/SQL 2 PL/SQL. 2.1 Variables. 2.2 PL/SQL Program Blocks

Teradata Database. SQL Reference. Data Types and Literals

Excel Database Management Microsoft Excel 2003

Oracle Database 11g SQL

A table is a collection of related data entries and it consists of columns and rows.

CONVERSION FUNCTIONS QUESTIONS

2. Which of the following declarations is invalid? Mark for Review (1) Points

Oracle Database: SQL and PL/SQL Fundamentals

Achieving Database Interoperability Across Data Access APIs through SQL Up-leveling

Participant Guide RP301: Ad Hoc Business Intelligence Reporting

Recognizing PL/SQL Lexical Units. Copyright 2007, Oracle. All rights reserved.

Event Viewer User Guide. Version 1.0

Examine the structure of the EMPLOYEES table: EMPLOYEE_ID NUMBER Primary Key FIRST_NAME VARCHAR2(25) LAST_NAME VARCHAR2(25)

9.1 SAS. SQL Query Window. User s Guide

Lab 9 Access PreLab Copy the prelab folder, Lab09 PreLab9_Access_intro

2. Which three statements about functions are true? (Choose three.) Mark for Review (1) Points

Export of audit trail events from Salto software. Version 2.0

ACCELL/SQL: Creating Reports with RPT Report Writer

Data Mining Commonly Used SQL Statements

A basic create statement for a simple student table would look like the following.

Information and Computer Science Department ICS 324 Database Systems Lab#11 SQL-Basic Query

Choosing a Data Model for Your Database

Business Online Transaction Import & Export (Download) File Formats REFERENCE DOCUMENT

Oracle Database: SQL and PL/SQL Fundamentals NEW

Beginning SQL, Differences Between Oracle and Microsoft

Oracle/SQL Tutorial 1

Creating and Using Databases with Microsoft Access

Package sjdbc. R topics documented: February 20, 2015

A Brief Introduction to MySQL

CHECKING AND VERIFYING TEMPORAL DATA VALIDITY USING VALID TIME TEMPORAL DIMENSION AND QUERIES IN ORACLE 12C

DBF Chapter. Note to UNIX and OS/390 Users. Import/Export Facility CHAPTER 7

Developing strategic analytics applications on OBICS PaaS. Or how we re doing so far

Review your answers, feedback, and question scores below. An asterisk (*) indicates a correct answer.

Market developments in Turkey

Oracle Internal & Oracle Academy

Review your answers, feedback, and question scores below. An asterisk (*) indicates a correct answer.

Transcription:

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 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 sensitıve; 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 fıeld with the SQL*Plus COLUMN command. The resultant column width is 80 characters by default. 1

Examples SELECT empno, TO_CHAR(hiredate, 'MM/YY') "ĐŞE GĐRĐŞ TARĐHĐ" FROM emp WHERE ename = 'BLAKE' ; EMP O E AME ĐŞE GĐRĐŞ 7698 BLAKE 05/81 SELECT ename, empno TO_CHAR( hiredate, 'fmmonth DD, YYYY' ) Hiredate FROM emp WHERE deptno = 30 E AME EMP O HIREDATE ALLEN 7499 Şubat 20, 1981 WARD 7521 Şubat 22, 1981 MARTIN 7654 Eylül 28, 1981 BLAKE 7698 Mayıs 1, 1981 TURNER 7844 Eylül 8, 1981 JAMES 7900 Aralık 3, 1981 6 rows selected. SELECT ename, hiredate, TO_CHAR( hiredate, 'fmdy.mon.yyy' ) FROM EMP; E AME HIREDATE TO_CHAR(HIREDATE,'FMDY.MO.YYY') SMITH 17/12/1980 ÇAR.Ara.980 ALLEN 20/02/1981 CUM.Şub.981... 14 rows selected. 2

Using TO_CHAR Function with Dates SELECT ename, hiredate, TO_CHAR(hiredate, 'DD Month YYYY' ) "İşeGiriş Tarihi" FROM EMP; E AME HIREDATE Đşe Giriş Tarihi SMITH 17/12/1980 17 Aralık 1980 ALLEN 20/02/1981 20 Şubat 1981 WARD 22/02/1981 22 Şubat 1981 JONES 02/04/1981 02 Nisan 1981 MARTIN 28/09/1981 28 Eylül 1981 BLAKE 01/05/1981 01 Mayıs 1981 CLARK 09/06/1981 09 Haziran 1981 SCOTT 09/12/1982 09 Aralık 1982 KING 17/11/1981 17 Kasım 1981 TURNER 08/09/1981 08 Eylül 1981 ADAMS 12/01/1983 12 Ocak 1983 JAMES 03/12/1981 03 Aralık 1981 FORD 03/12/1981 03 Aralık 1981 MILLER 23/01/1982 23 Ocak 1982 14 rows selected. 3

Example SELECT sysdate, TO_CHAR ( sysdate, 'fmdd.mm.yyyy fmhh:mi:ss PM' ) Zaman SYSDATE 18/03/2007 18.3.2007 06:23:02 ÖS ZAMA SELECT sysdate, TO_CHAR ( sysdate, 'fmdd.mm.yyyy fmhh:mi:ss AM' ) Zaman SYSDATE 18/03/2007 18.3.2007 06:23:02 ÖS ZAMA SELECT sysdate, TO_CHAR ( sysdate, 'fmdd.mm.yyyy fmhh12:mi:ss PM' ) Zaman SYSDATE 18/03/2007 18.3.2007 06:23:02 ÖS ZAMA 4

SELECT sysdate, TO_CHAR ( sysdate, 'fmdd.mm.yyyy fmhh24:mi:ss' ) Zaman SYSDATE 18/03/2007 18.3.2007 18:23:02 ZAMA SELECT sysdate, TO_CHAR ( sysdate, 'fmdd.mm.yyyy fmhh24:mi:ss AM' ) Zaman SYSDATE 18/03/2007 18.3.2007 18: 23:02 ÖS ZAMA 5

SELECT ename, TO_CHAR(hiredate, 'fmddspth "of" Month YYYY fmhh:mi:ss PM' ) HIREDATE FROM emp; E AME SMITH ALLEN HIREDATE Seventeenth of Aralık 1980 12:00:00 ÖÖ Twentieth of Şubat 1981 12:00:00 ÖÖ... 14 rows selected. 6

Examples : YY, CC, SCC SELECT TO_CHAR( TO_DATE('28-11-1942'), 'YY') Yüzyıl FROM dual; 42 YIL SELECT TO_CHAR( TO_DATE('28-11-1942'), 'CC') Yüzyıl FROM dual; 20 YÜZYIL SELECT TO_CHAR( TO_DATE('28-11-2942'), 'SCC') Binyıl FROM dual; 30 BI YIL 7

Examples : D, DD, DDD SELECT TO_CHAR( TO_DATE('28-11-1942'), 'fmd' ) "Day Of Week" 6 Day Of Week SELECT TO_CHAR( TO_DATE('28-11-1942'), 'fmdd' ) "Day Of Month" 28 Day Of Month SELECT TO_CHAR( TO_DATE('28-11-2942'), 'fmddd' ) "Day Of Year" 332 Day of Year 8

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" 2430692 Julian Day SELECT TO_CHAR( TO_DATE('28-11-1942'), 'fmbc' ) "BC Indicator" BC Indicator MS SELECT TO_CHAR( TO_DATE('28-11-1942'), 'fmbc' ) "AD Indicator" AD Indicator MS 9

SELECT ename, hiredate, TO_CHAR(hiredate, 'fmcc') FROM EMP ; E AME HIREDATE TO_CHA SMITH 17/12/1980 20 ALLEN 20/02/1981 20... 14 rows selected. SELECT TO_CHAR( TO_DATE('18-09-1972'), 'w' ) "Week of Month" Week of Month 3 SELECT TO_CHAR( TO_DATE('18-09-1972'), 'ww' ) "Week of Year" 38 Week of Month 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

Oracle date format Format mask CC Description Century SCC Century BC prefixed with - YYYY Year with 4 numbers SYYY Year BC prefixed with - IYYY YY RR YEAR ISO Year with 4 numbers Year with 2 numbers Year with 2 numbers with Y2k compatibility Year in characters SYEAR Year in characters, BC prefixed with - BC / AD BC/AD Indicator * Q Quarter in numbers (1,2,3,4) MM Month of year 01, 02...12 MONTH MON Month in characters (i.e. January) JAN, FEB WW Weeknumber (i.e. 2) W Weeknumber of the month (i.e. 3) IW Weeknumber of the year in ISO standard. DDD Day of year in numbers (i.e. 234) DD Day of the month in numbers (i.e. 28) D Day of week in numbers(i.e. 7) DAY FMDAY DY J Day of the week in characters (i.e. Monday) Day of the week in characters (i.e. Monday) Day of the week in short character description (i.e. SUN) Julian Day (number of days since January 1 4713 BC, where January 1 4713 BC is 1 in Oracle) HH Hour number of the day (1-12) HH12 Hour number of the day (1-12) HH24 Hour number of the day with 24Hours notation (1-24) AM PM AM or PM AM or PM MI Number of minutes (i.e. 59) SS Number of seconds (i.e. 59) SSSS Number of seconds this day. 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

Elements of Date Format Model Time efements format the time portion of the date. HH24:MI:SSAM 15:45:32 PM Add character strings by enclosing them in double quotation marks. DD "Of " MONTH 12 of OCTOBER Number suffixes spell out numbers. Ddspth fourteenth Time Formats Element AM or PM A.M. or P.M. Description Meridian indicator Meridian indicator with periods HH or HH12 or 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) 14

Oracle to_date function The oracle TO_DATE function converts a string in a specified format to an Oracle date format. Syntax to_date('formatted string'); to_date('formatted string','format string'); to_date('formatted string','format string','nls description'); returns a date using the default oracle date format returns a date using the format string specified returns a date using the format string specified and using the specified NLS settings Examples: SELECT to_date( '01-OCA-2007 ') FROM dual; 01/01/2007 TARIH SELECT to_date( '01-01-2007', 'DD-MM-YYYY') Tarih FROM dual; 01/01/2007 TARIH SELECT to_date( '31-12-2006 23:34:59', 'DD-MM-YYYY HH24:MI:SS ' ) 15 Tarih

FROM dual; TARIH 31/12/2006 SELECT to_date('01-jan-99', 'DD-MON-YY', 'nls_date_language = American') Tarih FROM dual; 01/01/2099 TARIH SELECT to_date('01-oca-99', 'DD-MON-YY', 'nls_date_language = Turkish') Tarih FROM dual; 01/01/2099 TARIH SELECT to_date ('2003/07/09', 'yyyy/dd/mm' ) 07/09/2003 TO_DATE('2 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

SELECT to_date ('93/07/09', 'yy/dd/mm' ) 07/09/2093 TARĐH 17

Oracle date format With the functions to_char and to_date, a date format can be used. Example: select to_char(sysdate,'dd/mm/yyyy HH24:MI:SS') from dual; will return something like: 24/03/2006 14:36:43 18

Oracle date format SELECT TO_DATE('Aralık 15, 1998', 'Month DD, YYYY') Tarih 15/12/1998 TARĐH SELECT TO_DATE('Aralık 15, 98', 'Month DD, YY') Tarih 15/12/2098 TARĐH SELECT TO_DATE('Aralık 15, 98', 'Month DD, RR') Tarih 15/12/1998 TARĐH SELECT TO_DATE('Aralık 15, 07', 'Month DD, RR') Tarih 15/12/2007 TARĐH 19

RR Date Format Current Year Specified Date RR Format YY Format 1995 1995 2001 2001 27-OCT-95 27-OCT-17 27-OCT-17 27-OCT-95 1995 2017 2017 1995 1995 1917 2017 2095 If two digits of the current year are: 5 3-49 If the specified two-digit year is: The return date is in the current century 0-99 The return date is in the century after the current öne 0-49 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 Element 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. Current year Given Date Interpreted (RR) Interpreted (YY) 1994 27-OCT-95 1995 1995 1994 27-OCT-17 2017 1917 2001 27-OCT-17 2017 2017 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; 18-Mar-2007 17:37:10 Current Time select to_char(sysdate, 'Dy DD-Mon-YYYY HH24:MI:SS') as "Current Time" from dual; PAZ 18-MAR-2007 05:46:41 Current Time SELECT TO_CHAR(CURRENT_DATE, 'Dy DD-MON-YYYY HH:MI:SS') FROM dual; TO_CHAR(CURRE T_DATE,'DD-MO -YYYYHH:MI:SS') PAZ 18-MAR-2007 05:46:41 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 ; Cuma, Haziran 19th, 1981 Cuma, Ağustos 21st, 1981 Cuma, Ağustos 28th, 1981 Cuma, Ekim 9th, 1981 Cuma, Kasım 6th, 1981 Cuma, Aralık 11th, 1981 Cuma, Mart 12th, 1982 Cuma, Nisan 2nd, 1982 Cuma, Mayıs 21st, 1982 Cuma, Haziran 4th, 1982 Cuma, Haziran 4th, 1982 Cuma, Temmuz 30th, 1982 Cuma, Haziran 10th, 1983 Cuma, Temmuz 15th, 1983 ext 6 Months Review 14 rows selected. 22

Practice 3 1. Write a query to display the current date. Label the column Date. 2. 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. 3. Run your query in the file p3q2.sql. 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