Edit D:\app\Administrator\product\11.2.0\dbhome_1\oc4j\j2ee\oc4j_applications\applications\em\em\online_help\tdddg\tdddg_selecting016.htm
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <script src="./callback.js" type="text/javascript"></script> <noscript>Your browser does not support JavaScript. This help page requires JavaScript to render correctly.</noscript> </head> <meta http-equiv="Content-Type" content="text/html; charset=us-ascii" /> <meta http-equiv="Content-Style-Type" content="text/css" /> <meta http-equiv="Content-Script-Type" content="text/javascript" /> <meta name="generator" content="Oracle DARB XHTML Converter (Mode = ohj/ohw) - Version 5.1.1 Build 005" /> <meta name="date" content="2009-04-21T9:46:23Z" /> <meta name="robots" content="noarchive" /> <meta name="doctitle" content="Using Conversion Functions in Queries" /> <meta name="relnum" content="11g Release 2 (11.2)" /> <meta name="partnum" content="E10766-01" /> <link rel="copyright" href="./dcommon/html/cpyr.htm" title="Copyright" type="text/html" /> <link rel="stylesheet" href="./dcommon/css/blafdoc.css" title="Oracle BLAFDoc" type="text/css" /> <link rel="contents" href="toc.htm" title="Contents" type="text/html" /> <link rel="prev" href="tdddg_selecting015.htm" title="Previous" type="text/html" /> <link rel="next" href="tdddg_selecting017.htm" title="Next" type="text/html" /> <title>Using Conversion Functions in Queries</title> </head> <body> <div class="zz-skip-header"><a href="#BEGIN">Skip Headers</a></div> <table class="simple oac_no_warn" summary="" cellspacing="0" cellpadding="0" width="100%"> <col width="86%" /> <col width="*" /> <tr valign="bottom"> <td align="left"></td> <td align="center"><a href="tdddg_selecting015.htm"><img width="24" height="24" src="./dcommon/gifs/leftnav.gif" alt="Previous" /><br /> <span class="icon">Previous</span></a> </td> <td align="center"><a href="tdddg_selecting017.htm"><img width="24" height="24" src="./dcommon/gifs/rightnav.gif" alt="Next" /><br /> <span class="icon">Next</span></a></td> </tr> </table> <p><a id="BCGFDEJD" name="BCGFDEJD"></a><a id="TDDDG22550" name="TDDDG22550"></a></p> <div class="sect2"><!-- infolevel="all" infotype="General" --> <h1>Using Conversion Functions in Queries</h1> <a name="BEGIN" id="BEGIN"></a> <p><a id="sthref136" name="sthref136"></a>Conversion functions convert one data type to another. The conversion functions that SQL supports are listed and described in <a href="javascript:open('http://www.oracle.com/pls/db112/lookup?id=SQLRF20034','newWindow').focus()"><span class="italic">Oracle Database SQL Language Reference</span></a>.</p> <p>The query in the following example uses the <code>TO_CHAR</code> function to convert <code>HIRE_DATE</code> values (which are of type <code>DATE</code>) to character values that have the format <code>FMMonth</code> <code>DD</code> <code>YYYY</code>. <code>FM</code> removes leading and trailing blanks from the month name. <code>FMMonth</code> <code>DD</code> <code>YYYY</code> is an example of a <span class="bold">format format model</span>.</p> <div class="example"><a id="CHDBBACC" name="CHDBBACC"></a><a id="TDDDG160" name="TDDDG160"></a> <p class="titleinexample">Converting Dates to Characters Using a Format Template</p> <pre xml:space="preserve" class="oac_no_warn"> SELECT LAST_NAME, HIRE_DATE, <span class="bold">TO_CHAR(HIRE_DATE, 'FMMonth DD YYYY')</span> "Date Started" FROM EMPLOYEES WHERE DEPARTMENT_ID = 100 ORDER BY LAST_NAME; </pre> <p>Result:</p> <pre xml:space="preserve" class="oac_no_warn"> LAST_NAME HIRE_DATE Date Started ------------------------- --------- ----------------- Chen 28-SEP-97 September 28 1997 Faviet 16-AUG-94 August 16 1994 Greenberg 17-AUG-94 August 17 1994 Popp 07-DEC-99 December 7 1999 Sciarra 30-SEP-97 September 30 1997 Urman 07-MAR-98 March 7 1998 6 rows selected. </pre></div> <!-- class="example" --> <p>The query in the following example uses the <code>TO_CHAR</code> function to convert <code>HIRE_DATE</code> values to character values that have the two standard formats <code>DS</code> (Date Short) and <code>DL</code> (Date Long).</p> <div class="example"><a id="CHDIBABI" name="CHDIBABI"></a><a id="TDDDG161" name="TDDDG161"></a> <p class="titleinexample">Converting Dates to Characters Using Standard Formats</p> <pre xml:space="preserve" class="oac_no_warn"> SELECT LAST_NAME, <span class="bold">TO_CHAR(HIRE_DATE, 'DS')</span> "Short Date", <span class="bold">TO_CHAR(HIRE_DATE, 'DL')</span> "Long Date" FROM EMPLOYEES WHERE DEPARTMENT_ID = 100 ORDER BY LAST_NAME; </pre> <p>Result:</p> <pre xml:space="preserve" class="oac_no_warn"> LAST_NAME Short Date Long Date ------------------------- ---------- ----------------------------- Chen 9/28/1997 Sunday, September 28, 1997 Faviet 8/16/1994 Tuesday, August 16, 1994 Greenberg 8/17/1994 Wednesday, August 17, 1994 Popp 12/7/1999 Tuesday, December 07, 1999 Sciarra 9/30/1997 Tuesday, September 30, 1997 Urman 3/7/1998 Saturday, March 07, 1998 6 rows selected. </pre></div> <!-- class="example" --> <p>The query in the following example uses the <code>TO_CHAR</code> function to convert <code>SALARY</code> (which are of type <code>NUMBER</code>) to character values that have the format <code>$99,999.99</code>. <code>$99,999.99</code> is an example of a <span class="bold">datetime format model</span>.</p> <div class="example"><a id="CHDEJJEJ" name="CHDEJJEJ"></a><a id="TDDDG162" name="TDDDG162"></a> <p class="titleinexample">Converting Numbers to Characters Using a Format Template</p> <pre xml:space="preserve" class="oac_no_warn"> SELECT LAST_NAME, <span class="bold">TO_CHAR(SALARY, '$99,999.99')</span> "Salary" FROM EMPLOYEES WHERE DEPARTMENT_ID = 100 ORDER BY SALARY; </pre> <p>Result:</p> <pre xml:space="preserve" class="oac_no_warn"> LAST_NAME Salary ------------------------- ----------- Popp $6,900.00 Sciarra $7,700.00 Urman $7,800.00 Chen $8,200.00 Faviet $9,000.00 Greenberg $12,000.00 6 rows selected. </pre></div> <!-- class="example" --> <p>The query in the following example uses the <code>TO_NUMBER</code> function to convert <code>POSTAL_CODE</code> values (which are of type <code>VARCHAR2</code>) to values of type <code>NUMBER</code>, which it uses in calculations.</p> <div class="example"><a id="CHDCGDHF" name="CHDCGDHF"></a><a id="TDDDG163" name="TDDDG163"></a> <p class="titleinexample">Converting Characters to Numbers</p> <pre xml:space="preserve" class="oac_no_warn"> SELECT CITY, POSTAL_CODE "Old Code", <span class="bold">TO_NUMBER(POSTAL_CODE) + 1</span> "New Code" FROM LOCATIONS WHERE COUNTRY_ID = 'US' ORDER BY POSTAL_CODE; </pre> <p>Result:</p> <pre xml:space="preserve" class="oac_no_warn"> CITY Old Code New Code ------------------------------ ------------ ---------- Southlake 26192 26193 South Brunswick 50090 50091 Seattle 98199 98200 South San Francisco 99236 99237 4 rows selected. </pre></div> <!-- class="example" --> <p>The query in the following example uses the <code>TO_DATE</code> function to convert a string of characters whose format is <code>Month</code> <code>dd,</code> <code>YYYY,</code> <code>HH:MI</code> <code>A.M.</code> to a <code>DATE</code> value.</p> <div class="example"><a id="CHDIHIHA" name="CHDIHIHA"></a><a id="TDDDG164" name="TDDDG164"></a> <p class="titleinexample">Converting a Character String to a Date</p> <pre xml:space="preserve" class="oac_no_warn"> SELECT <span class="bold">TO_DATE('January 5, 2007, 8:43 A.M.',</span> <span class="bold">'Month dd, YYYY, HH:MI A.M.')</span> "Date" FROM DUAL; </pre> <p>Result:</p> <pre xml:space="preserve" class="oac_no_warn"> Date --------- 05-JAN-07 </pre></div> <!-- class="example" --> <p>The query in the following example uses the <code>TO_TIMESTAMP</code> function to convert a string of characters whose format is <code>DD-Mon-RR</code> <code>HH24:MI:SS.FF</code> to a <code>TIMESTAMP</code> value.</p> <div class="example"><a id="CHDEJIAC" name="CHDEJIAC"></a><a id="TDDDG165" name="TDDDG165"></a> <p class="titleinexample">Converting a Character String to a Time Stamp</p> <pre xml:space="preserve" class="oac_no_warn"> SELECT <span class="bold">TO_TIMESTAMP('May 5, 2007, 8:43 A.M.',</span> <span class="bold">'Month dd, YYYY, HH:MI A.M.')</span> "Timestamp" FROM DUAL; </pre> <p>Result:</p> <pre xml:space="preserve" class="oac_no_warn"> Timestamp ------------------------------------------------------------------------------ 05-MAY-07 08.43.00.000000000 AM </pre></div> <!-- class="example" --> <div class="helpinfonotealso"> <h2>Related Topics</h2> <p><a href="javascript:open('http://www.oracle.com/pls/db112/lookup?id=SQLRF20034','newWindow').focus()"><span class="italic">Oracle Database SQL Language Reference</span></a> for more information about SQL conversion functions</p> <p><a href="javascript:open('http://www.oracle.com/pls/db112/lookup?id=SQLRF06128','newWindow').focus()"><span class="italic">Oracle Database SQL Language Reference</span></a> for more information about the <code>TO_CHAR</code> function</p> <p><a href="javascript:open('http://www.oracle.com/pls/db112/lookup?id=SQLRF06140','newWindow').focus()"><span class="italic">Oracle Database SQL Language Reference</span></a> for more information about the <code>TO_NUMBER</code> function</p> <p><a href="javascript:open('http://www.oracle.com/pls/db112/lookup?id=SQLRF06132','newWindow').focus()"><span class="italic">Oracle Database SQL Language Reference</span></a> for more information about the <code>TO_DATE</code> function</p> <p><a href="javascript:open('http://www.oracle.com/pls/db112/lookup?id=SQLRF06142','newWindow').focus()"><span class="italic">Oracle Database SQL Language Reference</span></a> for more information about the <code>TO_TIMESTAMP</code> function</p> <p><a href="tdddg_globalization019.htm#CACIFHHI">About the NLS_DATE_FORMAT Parameter</a></p> <p><a href="tdddg_selecting010.htm#BCGCCGJF">Using Operators and Functions in Queries</a></p> </div> </div> <!-- class="sect2" --> <!-- Start Footer --> <div class="footer"> <table class="simple oac_no_warn" summary="" cellspacing="0" cellpadding="0" width="100%"> <col width="86%" /> <col width="*" /> <tr> <td align="left"><span class="copyrightlogo">Copyright © 1996, 2009, Oracle and/or its affiliates. All rights reserved.</span><br /> <a href="./dcommon/html/cpyr.htm"><span class="copyrightlogo">Legal Notices</span></a></td> <td align="center"><a href="tdddg_selecting015.htm"><img width="24" height="24" src="./dcommon/gifs/leftnav.gif" alt="Previous" /><br /> <span class="icon">Previous</span></a> </td> <td align="center"><a href="tdddg_selecting017.htm"><img width="24" height="24" src="./dcommon/gifs/rightnav.gif" alt="Next" /><br /> <span class="icon">Next</span></a></td> </tr> </table> </div> <!-- class="footer" --> </body> </html>
Ms-Dos/Windows
Unix
Write backup
jsp File Browser version 1.2 by
www.vonloesch.de