You are on page 1of 25

Conversion Functions

Copyright © 2011, Oracle. All rights reserved.

Conversion Functions

What Will I Learn?
Objectives In this lesson, you will learn to: • Provide an example of an explicit data-type conversion and an implicit data-type conversion • Explain why it is important, from a business perspective, for a language to have built-in data-conversion capabilities • Construct a SQL query that correctly applies TO_CHAR, TO_NUMBER and TO_DATE single-row functions to produce a desired result • Apply the appropriate date and/or character format model to produce a desired output • Explain and apply the use of YYYY and RRRR to return the correct year as stored in the database
Copyright © 2011, Oracle. All rights reserved. 2

3 . Copyright © 2011. there are software programs available to capitalize and color text. It would be difficult to read. For databases. underline. format and display changes are done using conversion functions. These functions are able to display numbers as local currency. bold. Fortunately. format dates in a variety of formats. All rights reserved. Oracle. center and add graphics. and keep track of what century a date refers to. display time to the second.Conversion Functions Why Learn It? Purpose Imagine having to read all your school books in text files with no paragraphs and no capitalization.

4 . All rights reserved. Oracle. For this lesson. you will use: VARCHAR2 CHAR NUMBER DATE Copyright © 2011.Conversion Functions Tell Me / Show Me Data Types When a table is created for a database. In SQL. These data types define the domain of values that each column can contain. there are several different data types. the SQL programmer must define what kind of data will be stored in each field of the table.

Conversion Functions Tell Me / Show Me Data Types Described VARCHAR2: Used for character data of variable length. Copyright © 2011. 5 . including numbers. dashes and special characters. DATE: Used for date and time values. Internally. or other nonnumeric data are allowed. No dashes. Oracle stores dates as numbers and by default DATE information is displayed as DD-MON-YY (for example. text. Currency is stored as a number data type. 19-JUN-04). CHAR: Used for text and character data of fixed length. including numbers. All rights reserved. special characters and dashes. NUMBER: Used to store variable-length numeric data. Oracle.

Oracle. The four data-type conversion functions you will learn are: • To convert date data type to character data type • To convert number data type to character data type • To convert character data type to number data type • To convert character data type to date data types DATE TO_CHAR Copyright © 2011. 6 . All rights reserved. it is always best TO_CHAR to explicitly make date-type conversions to ensure reliability in SQL statements.Conversion Functions Tell Me / Show Me EXPLICIT DATE-TYPE CONVERSION Type Conversion TO_NUMBER TO_DATE The Oracle Server can internally convert VARCHAR2 and CHAR data to NUMBER and DATE data types. It can convert NUMBER CHARACTER NUMBER and DATE data back to CHARACTER datatype. Although this is a convenient feature.

Any valid date format element can be included. All rights reserved. Copyright © 2011. (1st. 2nd. 3rd and so on) Use double quotation marks to add character strings to format models. Use th to have the number appear as an ordinal. 'format model you specify') • • • • • • • The „format model‟ must be enclosed in single quotation marks and is case-sensitive. The function to accomplish this task is: TO_CHAR (date column name.Conversion Functions Tell Me / Show Me Date Conversion to Character Data It is often desirable to convert dates stored in a database in the default DDMON-YY format to another format specified by you. Separate the date value from the „format model with a comma. 7 . Use an fm element to remove padded blanks or remove leading zeroes from the output. Use sp to spell out a number. Oracle.

YYYY YEAR MM MONTH MON DY DAY DD Full year in numbers Year spelled out Two-digit value for month Full name of the month Three-letter abbreviation of the month Three-letter abbreviation of the day of the week Full name of the day of the week Numeric day of the month 15:45:32 PM 12 of October FOURTEENTH Fourteenth HH24:MI:SS AM DD “of” MONTH DDspth Ddspth ddspth DDD or DD or D fourteenth Day of year. minutes (MI).Conversion Functions Tell Me / Show Me Date Conversion to Character Data (continued) The tables show the different format models that can be used. and AM or PM can also be formatted. All rights reserved. Oracle. month or week Copyright © 2011. seconds (SS). note that hours (HH). 8 . When specifying time elements.

Oracle. TO_CHAR(hire_date. the format model using fm would have returned May 4. Copyright © 2011. 9 . 'fmMonth dd. All rights reserved. RRRR') FROM employees. SELECT TO_CHAR (hire_date. YYYY') FROM employees.Conversion Functions Tell Me / Show Me Date Conversion to Character Data (continued) If an employee‟s hire date was 04-MAY-04. 2004. 'MONTH DD. suppressing the leading zero. What will be the output of the following query? SELECT employee_id.

Friday. 2007 AUG 6. All rights reserved. month or week Copyright © 2011.Conversion Functions Tell Me / Show Me Date and Time Format Models The following tables show variations of the format models that can be used with dates and time. 2007 August 6th. Can you identify the format models used to produce today's date as the following output? August 6th. Two Thousand Seven YYYY YEAR MM MONTH MON DY DAY DD HH24:MI:SS AM DD “of” MONTH DDspth Ddspth ddspth DDD or DD or D Full year in numbers Year spelled out Two-digit value for month Full name of the month Three-letter abbreviation of the month Three-letter abbreviation of the day of the week Full name of the day of the week Numeric day of the month 15:45:32 PM 12 of October FOURTEENTH Fourteenth fourteenth Day of year. 10 . 2007 August 06. Oracle.

The SQL function that you use to convert columns of number data to a desired format is: TO_CHAR(number. no commas. 'format model') Copyright © 2011. Oracle. you first need to convert the number to a character format. This conversion is especially useful with concatenation. 11 . This means that there are no currency signs/symbols. no decimals or other formatting. To add formatting.Conversion Functions Tell Me / Show Me Number Conversion to Character Data (VARCHAR2) Numbers stored in the database have no formatting. All rights reserved.

12 . Oracle.00 1. .999') COST FROM d_events. SELECT TO_CHAR(cost. MI PR EEEE V DESCRIPTION Numeric position (# of 9‟s determine width) Display leading zeros Floating dollar sign Floating local currency symbol Decimal point in position specified Comma in position specified Minus signs to right (negative values) Parenthesize negative numbers Scientific notation ( must have four EEEE) Multiply by 10 n times (n= number of 9‟s after V) Display zero values as blank. '$99.999 999999MI 999999PR 99.234 1234<1234> 1.00 Copyright © 2011.Conversion Functions Tell Me / Show Me Number Conversion to Character Data (VARCHAR2) (continued) The table illustrates some of the format elements available to use with TO_CHAR functions. not 0 EXAMPLE 999999 099999 $999999 L999999 999999.000 B B9999. All rights reserved.99 999.999EEEE 9999V99 RESULT 1234 001234 $1234 FF1234 1234. ELEMENT 9 0 $ L .23E+03 9999V99 COST $8.000 $10.99 1234.

000. MI PR 999. DESCRIPTION Numeric position (# of 9‟s determine width) Display leading zeros Floating dollar sign Floating local currency symbol Decimal point in position specified Comma in position specified Minus signs to right (negative values) Parenthesize negative numbers Scientific notation ( must have four EEEE) Multiply by 10 n times (n= number of 9‟s after V) Display zero values as blank.00 4.99 1234.00 0004422 EEEE V B B9999.00 . Oracle.23E+03 9999V99 $3000. 13 .234 1234<1234> 1.Conversion Functions Tell Me / Show Me Number Conversion to Character Data (VARCHAR2) (continued) Can you identify the format models used to produce the following output? ELEMENT 9 0 $ L .999EEEE 9999V99 1.500 9.999 999999MI 999999PR 99. All rights reserved. not 0 EXAMPLE 999999 099999 $999999 L999999 999999.99 RESULT 1234 001234 $1234 FF1234 1234.00 Copyright © 2011.

Conversion Functions Tell Me / Show Me Character Conversion to Number It is often desirable to convert a character string to a number. without the single quotes. 14 . The "450" was stored in the database as character data. All rights reserved. The function for this conversion is: TO_NUMBER(character string. You cannot perform calculations with character data. SELECT TO_NUMBER(„450‟) AS “Number Change” FROM DUAL. 'format model') This converts a nonnumeric value such as "450" to a number. The single quotes are characters. and the following query converts it to a number so that arithmetic operations can be performed. Oracle. Number Change 450 Copyright © 2011.

15 . if the format model does not match the actual number returned by the database. '9999') + 10 AS "Number Change" FROM DUAL. Oracle Application Express will return an Oracle Error – Invalid Number. Oracle. All rights reserved. Copyright © 2011. Number Change 460 SQL*Plus displays a string of hash signs (#) in place of a whole number whose digits exceed the number of digits provided in the format model and rounds numbers to the decimal place provided in the format model.Conversion Functions Tell Me / Show Me Character Conversion to Number (continued) SELECT TO_NUMBER('450'.

2001" and converts it to a date value. 16 . 2001'. 'Month dd. 'format model') This conversion takes a nondate value character string such as "November 3. All rights reserved. Oracle. The format model tells the server what the character string "looks like": TO_DATE('November 3.Conversion Functions Tell Me / Show Me Character Conversion to Date To convert a character string to a date format. RRRR') will return 03-NOV-01 Copyright © 2011. use: TO_DATE('character string'.

" The fx format model matches the character argument as it also has no space between "Mon" and "DD. 'fxMonDD. 17 . the fx (format exact) modifier specifies exact matching for the character argument and the date format model.RRRR') AS "Convert" FROM DUAL. CONVERT 10-MAY-89 Copyright © 2011. Oracle. In the following example." SELECT TO_DATE('May10. note that "May10" has no space between ''May" and "10. All rights reserved.1989'.Conversion Functions Tell Me / Show Me Character Conversion to Date (continued) When making a character-to-date conversion.

numbers in the character argument can omit leading zeros. the Oracle Server ignores extra blanks. • Numeric data in the character argument must have the same number of digits as the corresponding element in the format model. • The character argument cannot have extra blanks. Oracle. 18 . Copyright © 2011. Without fx. Without fx. All rights reserved.Conversion Functions Tell Me / Show Me fx Modifier Rules The fx modifier rules are: • Punctuation and quoted text in the character argument must match the corresponding parts of the format model exactly (except for case).

Oracle. Oracle has a way to keep these dates stored and retrievable with the correct century. 19 .Conversion Functions Tell Me / Show Me RR Date Format and YY Date Format Current Year 1995 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 It hasn't been that long since the century changed from 1900 to 2000. 1995 2001 2001 If the specified two-digit year is: 0-49 If two digits of the current year are: 0-49 The return date is in the current century The return date is in the century after the current one 50-99 The return date is in the century before the current one The return date is in the current century 50-99 Copyright © 2011. Fortunately. 1900 or January 2. 2000. Along with this change came considerable confusion as to whether a date written as 02-JAN-00 would be interpreted as January 2. All rights reserved.

So.Conversion Functions Tell Me / Show Me A Few Simple Rules If the date format is specified with the YY or YYYY format. if the year is 1995 and you use the YY or YYYY format. you will get 2089! Maybe not what you intended. All rights reserved. the return value will be in the same century as the current century. all is well and the dates will be in the 1900s. However. 20 . if the current year is 2004 and you use the YY or YYYY format for a date such as 1989. 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 Copyright © 2011. Oracle.

All rights reserved. 21 .Conversion Functions Tell Me / Show Me A Few Simple Rules (continued) If the date format is specified with the RR or RRRR format. If the current year is between 0049: • Dates from 0-49: The date will be in the current century • Dates from 50-99: The date will be in the last century If the current year is between 5099: • Dates from 0-49: The date will be in next century • Dates from 50-99: The date will be in current century If the specified two-digit year is: 0-49 If two digits of the current year are: 0-49 The return date is in the current century The return date is in the century after the current one 50-99 The return date is in the century before the current one The return date is in the current century 50-99 Copyright © 2011. Oracle. the return value has two possibilities.

Oracle. 22 .Conversion Functions Tell Me / Show Me A Few Simple Rules (continued) When I query my employee database using the following statement. Did I make a mistake? SELECT last_name. TO_CHAR(hire_date.'DDMon-YY'). 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: 0-49 The return date is in the current century The return date is in the century after the current one 50-99 The return date is in the century before the current one The return date is in the current century 50-99 Copyright © 2011. I know there are only a few employees who were hired before 1990. 'DDMon-YY') FROM employees WHERE hire_date < TO_DATE('01-Jan-90'. All rights reserved. it returns every row in the table.

All rights reserved. Oracle. 23 .Conversion Functions Tell Me / Show Me Terminology Key terms used in this lesson include: CHAR DATE DD date format Conversion function fm NUMBER RR date format TO_CHAR TO_DATE TO_NUMBER VARCHAR2 Copyright © 2011.

All rights reserved. from a business perspective. Oracle. TO_NUMBER and TO_DATE single-row functions to produce a desired result • Apply the appropriate date and/or character format model to produce a desired output • Explain and apply the use of YYYY and RRRR to return the correct year as stored in the database Copyright © 2011. for a language to have built-in dataconversion capabilities • Construct a SQL query that correctly applies TO_CHAR. you have learned to: • Provide an example of an explicit data-type conversion and an implicit data-type conversion • Explain why it is important. 24 .Conversion Functions Summary Objectives Summarized In this lesson.

25 . All rights reserved.Conversion Functions Summary Practice Guide The link for the lesson practice guide can be found in the course resources in Section 0. Copyright © 2011. Oracle.