<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>http://wiki.zcubes.com/index.php?action=history&amp;feed=atom&amp;title=Yurttas%2FPL%2FDBL%2Foracle%2FF%2FR%2For-time.html</id>
	<title>Yurttas/PL/DBL/oracle/F/R/or-time.html - Revision history</title>
	<link rel="self" type="application/atom+xml" href="http://wiki.zcubes.com/index.php?action=history&amp;feed=atom&amp;title=Yurttas%2FPL%2FDBL%2Foracle%2FF%2FR%2For-time.html"/>
	<link rel="alternate" type="text/html" href="http://wiki.zcubes.com/index.php?title=Yurttas/PL/DBL/oracle/F/R/or-time.html&amp;action=history"/>
	<updated>2026-04-28T04:39:48Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.35.2</generator>
	<entry>
		<id>http://wiki.zcubes.com/index.php?title=Yurttas/PL/DBL/oracle/F/R/or-time.html&amp;diff=82548&amp;oldid=prev</id>
		<title>MassBot1: Created page with &quot;=&lt;font size=&quot;+3&quot;&gt;Oracle Dates and Times&lt;/font&gt;   ----=    * Overview  * DATE Format  * yu...&quot;</title>
		<link rel="alternate" type="text/html" href="http://wiki.zcubes.com/index.php?title=Yurttas/PL/DBL/oracle/F/R/or-time.html&amp;diff=82548&amp;oldid=prev"/>
		<updated>2013-11-05T05:35:39Z</updated>

		<summary type="html">&lt;p&gt;Created page with &amp;quot;=&amp;lt;font size=&amp;quot;+3&amp;quot;&amp;gt;Oracle Dates and Times&amp;lt;/font&amp;gt;   ----=    * &lt;a href=&quot;/index.php?title=Yurttas/PL/DBL/oracle/F/R/&amp;amp;action=edit&amp;amp;redlink=1&quot; class=&quot;new&quot; title=&quot;Yurttas/PL/DBL/oracle/F/R/ (page does not exist)&quot;&gt;Overview&lt;/a&gt;  * &lt;a href=&quot;/index.php?title=Yurttas/PL/DBL/oracle/F/R/&amp;amp;action=edit&amp;amp;redlink=1&quot; class=&quot;new&quot; title=&quot;Yurttas/PL/DBL/oracle/F/R/ (page does not exist)&quot;&gt;DATE Format&lt;/a&gt;  * yu...&amp;quot;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;=&amp;lt;font size=&amp;quot;+3&amp;quot;&amp;gt;Oracle Dates and Times&amp;lt;/font&amp;gt;   ----=&lt;br /&gt;
&lt;br /&gt;
* [[yurttas/PL/DBL/oracle/F/R/#overview|Overview]]&lt;br /&gt;
* [[yurttas/PL/DBL/oracle/F/R/#date format|DATE Format]]&lt;br /&gt;
* [[yurttas/PL/DBL/oracle/F/R/#the current time|The Current Time]]&lt;br /&gt;
* [[yurttas/PL/DBL/oracle/F/R/#operations on date|Operations on DATE]]&lt;br /&gt;
* [[yurttas/PL/DBL/oracle/F/R/#further information|Further Information]]&lt;br /&gt;
&lt;br /&gt;
===---- Overview===&lt;br /&gt;
&lt;br /&gt;
Oracle does support both date and time, albeit differently from the SQL2 standard. Rather than using two separate entities, date and time, Oracle only uses one, &amp;lt;tt&amp;gt;DATE&amp;lt;/tt&amp;gt;. The &amp;lt;tt&amp;gt;DATE&amp;lt;/tt&amp;gt; type is stored in a special internal format that includes not just the month, day, and year, but also &amp;lt;br /&amp;gt;the hour, minute, and second.&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;tt&amp;gt;DATE&amp;lt;/tt&amp;gt; type is used in the same way as other built-in types such as &amp;lt;tt&amp;gt;INT&amp;lt;/tt&amp;gt;. For example, the following SQL statement creates a relation with an attribute of type &amp;lt;tt&amp;gt;DATE&amp;lt;/tt&amp;gt;:&lt;br /&gt;
&lt;br /&gt;
 create table x(a int, b date);&lt;br /&gt;
&lt;br /&gt;
===---- &amp;lt;tt&amp;gt;DATE&amp;lt;/tt&amp;gt; Format===&lt;br /&gt;
&lt;br /&gt;
When a &amp;lt;tt&amp;gt;DATE&amp;lt;/tt&amp;gt; value is displayed, Oracle must first convert that value from the special internal format to a printable string. The conversion is done by a function &amp;lt;tt&amp;gt;TO_CHAR&amp;lt;/tt&amp;gt;, according to a &amp;lt;tt&amp;gt;DATE&amp;lt;/tt&amp;gt; ''format''. Oracle's default format for &amp;lt;tt&amp;gt;DATE&amp;lt;/tt&amp;gt; is &amp;quot;&amp;lt;tt&amp;gt;DD-MON-YY&amp;lt;/tt&amp;gt;&amp;quot;. Therefore, when you issue the query&lt;br /&gt;
&lt;br /&gt;
 select b from x;&lt;br /&gt;
&lt;br /&gt;
you will see something like:&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;B&lt;br /&gt;
 ---------&lt;br /&gt;
 01-APR-98&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Whenever a &amp;lt;tt&amp;gt;DATE&amp;lt;/tt&amp;gt; value is displayed, Oracle will call &amp;lt;tt&amp;gt;TO_CHAR&amp;lt;/tt&amp;gt; automatically with the default &amp;lt;tt&amp;gt;DATE&amp;lt;/tt&amp;gt; format. However, you may override the default behavior by calling &amp;lt;tt&amp;gt;TO_CHAR&amp;lt;/tt&amp;gt; explicitly with your own &amp;lt;tt&amp;gt;DATE&amp;lt;/tt&amp;gt; format. For example,&lt;br /&gt;
&lt;br /&gt;
 select to_char(b, 'YYYY/MM/DD') as b&lt;br /&gt;
 from x;&lt;br /&gt;
&lt;br /&gt;
returns the result:&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;B&lt;br /&gt;
 ---------------------------------------------------------------------------&lt;br /&gt;
 1998/04/01&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The general usage of &amp;lt;tt&amp;gt;TO_CHAR&amp;lt;/tt&amp;gt; is:&lt;br /&gt;
&lt;br /&gt;
 TO_CHAR(&amp;amp;lt;date&amp;amp;gt;, '&amp;amp;lt;format&amp;amp;gt;')&lt;br /&gt;
&lt;br /&gt;
where the &amp;lt;tt&amp;gt;&amp;amp;lt;format&amp;amp;gt;&amp;lt;/tt&amp;gt; string can be formed from over 40 options. Some of the more popular ones include: &amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
{| width=&amp;quot;100%&amp;quot; border=&amp;quot;BORDER&amp;quot;&lt;br /&gt;
| &amp;lt;tt&amp;gt;MM&amp;lt;/tt&amp;gt;&lt;br /&gt;
| Numeric month (''e.g.'', &amp;lt;tt&amp;gt;07&amp;lt;/tt&amp;gt;)&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;tt&amp;gt;MON&amp;lt;/tt&amp;gt;&lt;br /&gt;
| Abbreviated month name (''e.g.'', &amp;lt;tt&amp;gt;JUL&amp;lt;/tt&amp;gt;)&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;tt&amp;gt;MONTH&amp;lt;/tt&amp;gt;&lt;br /&gt;
| Full month name (''e.g.'', &amp;lt;tt&amp;gt;JULY&amp;lt;/tt&amp;gt;)&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;tt&amp;gt;DD&amp;lt;/tt&amp;gt;&lt;br /&gt;
| Day of month (''e.g.'', &amp;lt;tt&amp;gt;24&amp;lt;/tt&amp;gt;)&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;tt&amp;gt;DY&amp;lt;/tt&amp;gt;&lt;br /&gt;
| Abbreviated name of day (''e.g.'', &amp;lt;tt&amp;gt;FRI&amp;lt;/tt&amp;gt;)&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;tt&amp;gt;YYYY&amp;lt;/tt&amp;gt;&lt;br /&gt;
| 4-digit year (''e.g.'', &amp;lt;tt&amp;gt;1998&amp;lt;/tt&amp;gt;)&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;tt&amp;gt;YY&amp;lt;/tt&amp;gt;&lt;br /&gt;
| Last 2 digits of the year (''e.g.'', &amp;lt;tt&amp;gt;98&amp;lt;/tt&amp;gt;)&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;tt&amp;gt;RR&amp;lt;/tt&amp;gt;&lt;br /&gt;
| Like &amp;lt;tt&amp;gt;YY&amp;lt;/tt&amp;gt;, but &amp;lt;tt&amp;gt;06&amp;lt;/tt&amp;gt; is considered &amp;lt;tt&amp;gt;2006&amp;lt;/tt&amp;gt; instead of &amp;lt;tt&amp;gt;1906&amp;lt;/tt&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;tt&amp;gt;AM&amp;lt;/tt&amp;gt; (or &amp;lt;tt&amp;gt;PM&amp;lt;/tt&amp;gt;)&lt;br /&gt;
| Meridian indicator&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;tt&amp;gt;HH&amp;lt;/tt&amp;gt;&lt;br /&gt;
| Hour of day (&amp;lt;tt&amp;gt;1&amp;lt;/tt&amp;gt;-&amp;lt;tt&amp;gt;12&amp;lt;/tt&amp;gt;)&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;tt&amp;gt;HH24&amp;lt;/tt&amp;gt;&lt;br /&gt;
| Hour of day (&amp;lt;tt&amp;gt;0&amp;lt;/tt&amp;gt;-&amp;lt;tt&amp;gt;23&amp;lt;/tt&amp;gt;)&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;tt&amp;gt;MI&amp;lt;/tt&amp;gt;&lt;br /&gt;
| Minute (&amp;lt;tt&amp;gt;0&amp;lt;/tt&amp;gt;-&amp;lt;tt&amp;gt;59&amp;lt;/tt&amp;gt;)&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;tt&amp;gt;SS&amp;lt;/tt&amp;gt;&lt;br /&gt;
| Second (&amp;lt;tt&amp;gt;0&amp;lt;/tt&amp;gt;-&amp;lt;tt&amp;gt;59&amp;lt;/tt&amp;gt;)&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
You have just learned how to output a &amp;lt;tt&amp;gt;DATE&amp;lt;/tt&amp;gt; value using &amp;lt;tt&amp;gt;TO_CHAR&amp;lt;/tt&amp;gt;. Now what about inputting a &amp;lt;tt&amp;gt;DATE&amp;lt;/tt&amp;gt; value? This is done through a function called &amp;lt;tt&amp;gt;TO_DATE&amp;lt;/tt&amp;gt;, which converts a string to a &amp;lt;tt&amp;gt;DATE&amp;lt;/tt&amp;gt; value, again according to the &amp;lt;tt&amp;gt;DATE&amp;lt;/tt&amp;gt; format. Normally, you do not have to call &amp;lt;tt&amp;gt;TO_DATE&amp;lt;/tt&amp;gt; explicitly: Whenever Oracle expects a &amp;lt;tt&amp;gt;DATE&amp;lt;/tt&amp;gt; value, it will automatically convert your input string using &amp;lt;tt&amp;gt;TO_DATE&amp;lt;/tt&amp;gt; according to the default &amp;lt;tt&amp;gt;DATE&amp;lt;/tt&amp;gt; format &amp;quot;&amp;lt;tt&amp;gt;DD-MON-YY&amp;lt;/tt&amp;gt;&amp;quot;. For example, to insert a tuple with a &amp;lt;tt&amp;gt;DATE&amp;lt;/tt&amp;gt; attribute, you can simply type:&lt;br /&gt;
&lt;br /&gt;
 insert into x values(99, '31-may-98');&lt;br /&gt;
&lt;br /&gt;
Alternatively, you may use &amp;lt;tt&amp;gt;TO_DATE&amp;lt;/tt&amp;gt; explicitly:&lt;br /&gt;
&lt;br /&gt;
 insert into x&lt;br /&gt;
 values(99, to_date('1998/05/31:12:00:00AM', 'yyyy/mm/dd:hh:mi:ssam'));&lt;br /&gt;
&lt;br /&gt;
The general usage of &amp;lt;tt&amp;gt;TO_DATE&amp;lt;/tt&amp;gt; is:&lt;br /&gt;
&lt;br /&gt;
 TO_DATE(&amp;amp;lt;string&amp;amp;gt;, '&amp;amp;lt;format&amp;amp;gt;')&lt;br /&gt;
&lt;br /&gt;
where the &amp;lt;tt&amp;gt;&amp;amp;lt;format&amp;amp;gt;&amp;lt;/tt&amp;gt; string has the same options as in &amp;lt;tt&amp;gt;TO_CHAR&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Finally, you can change the default &amp;lt;tt&amp;gt;DATE&amp;lt;/tt&amp;gt; format of Oracle from &amp;quot;&amp;lt;tt&amp;gt;DD-MON-YY&amp;lt;/tt&amp;gt;&amp;quot; to something you like by issuing the following command in &amp;lt;tt&amp;gt;sqlplus&amp;lt;/tt&amp;gt;:&lt;br /&gt;
&lt;br /&gt;
 alter session set NLS_DATE_FORMAT='&amp;amp;lt;my_format&amp;amp;gt;';&lt;br /&gt;
&lt;br /&gt;
The change is only valid for the current &amp;lt;tt&amp;gt;sqlplus&amp;lt;/tt&amp;gt; session.&lt;br /&gt;
&lt;br /&gt;
===---- The Current Time===&lt;br /&gt;
&lt;br /&gt;
The built-in function &amp;lt;tt&amp;gt;SYSDATE&amp;lt;/tt&amp;gt; returns a &amp;lt;tt&amp;gt;DATE&amp;lt;/tt&amp;gt; value containing the current date and time on your system. For example,&lt;br /&gt;
&lt;br /&gt;
 select to_char(sysdate, 'Dy DD-Mon-YYYY HH24:MI:SS') as &amp;quot;Current Time&amp;quot;&lt;br /&gt;
 from dual;&lt;br /&gt;
&lt;br /&gt;
returns&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;Current Time&lt;br /&gt;
 ---------------------------------------------------------------------------&lt;br /&gt;
 Tue 21-Apr-1998 21:18:27&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
which is the time when I was preparing this document &amp;lt;tt&amp;gt;:-)&amp;lt;/tt&amp;gt; Two interesting things to note here:&lt;br /&gt;
&lt;br /&gt;
* You can use double quotes to make names case sensitive (by default, SQL is case insensitive), or to force spaces into names. Oracle will treat everything inside the double quotes literally as a single name. In this example, if &amp;lt;tt&amp;gt;&amp;quot;Current Time&amp;quot;&amp;lt;/tt&amp;gt; is not quoted, it would have been interpreted as ''two'' case insensitive names &amp;lt;tt&amp;gt;CURRENT&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;TIME&amp;lt;/tt&amp;gt;, which would actually cause a syntax error.&lt;br /&gt;
* &amp;lt;tt&amp;gt;DUAL&amp;lt;/tt&amp;gt; is built-in relation in Oracle which serves as a dummy relation to put in the &amp;lt;tt&amp;gt;FROM&amp;lt;/tt&amp;gt; clause when nothing else is appropriate. For example, try &amp;quot;&amp;lt;tt&amp;gt;select 1+2 from dual;&amp;lt;/tt&amp;gt;&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
===---- Operations on &amp;lt;tt&amp;gt;DATE&amp;lt;/tt&amp;gt;===&lt;br /&gt;
&lt;br /&gt;
You can compare &amp;lt;tt&amp;gt;DATE&amp;lt;/tt&amp;gt; values using the standard comparison operators such as &amp;lt;tt&amp;gt;=&amp;lt;/tt&amp;gt;, &amp;lt;tt&amp;gt;!=&amp;lt;/tt&amp;gt;, &amp;lt;tt&amp;gt;&amp;amp;gt;&amp;lt;/tt&amp;gt;, ''etc.''&lt;br /&gt;
&lt;br /&gt;
You can subtract two &amp;lt;tt&amp;gt;DATE&amp;lt;/tt&amp;gt; values, and the result is a &amp;lt;tt&amp;gt;FLOAT&amp;lt;/tt&amp;gt; which is the number of days between the two &amp;lt;tt&amp;gt;DATE&amp;lt;/tt&amp;gt; values. In general, the result may contain a fraction because &amp;lt;tt&amp;gt;DATE&amp;lt;/tt&amp;gt; also has a time component. For obvious reasons, adding, multiplying, and dividing two &amp;lt;tt&amp;gt;DATE&amp;lt;/tt&amp;gt; values are not allowed.&lt;br /&gt;
&lt;br /&gt;
You can add and subtract numbers from a &amp;lt;tt&amp;gt;DATE&amp;lt;/tt&amp;gt; value, and these numbers will be interpreted as numbers of days. For example, &amp;lt;tt&amp;gt;SYSDATE+1&amp;lt;/tt&amp;gt; will be tomorrow. You cannot multiply or divide &amp;lt;tt&amp;gt;DATE&amp;lt;/tt&amp;gt; values.&lt;br /&gt;
&lt;br /&gt;
With the help of &amp;lt;tt&amp;gt;TO_CHAR&amp;lt;/tt&amp;gt;, string operations can be used on &amp;lt;tt&amp;gt;DATE&amp;lt;/tt&amp;gt; values as well. For example, &amp;lt;tt&amp;gt;to_char(&amp;amp;lt;date&amp;amp;gt;, 'DD-MON-YY') like '%JUN%'&amp;lt;/tt&amp;gt; evaluates to true if &amp;lt;tt&amp;gt;&amp;amp;lt;date&amp;amp;gt;&amp;lt;/tt&amp;gt; is in June.&lt;br /&gt;
&lt;br /&gt;
===---- Further Information===&lt;br /&gt;
&lt;br /&gt;
Inside &amp;lt;tt&amp;gt;sqlplus&amp;lt;/tt&amp;gt;, type&lt;br /&gt;
&lt;br /&gt;
 help date;&lt;br /&gt;
&lt;br /&gt;
for more information. &amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&amp;lt;font size=&amp;quot;-2&amp;quot;&amp;gt;This document is written originally by Kristian Widjaja for Prof. Jeff Ullman's CS145 class in Autumn, 1997; revised by Jun Yang for Prof. Jennifer Widom's CS145 class in Spring, 1998.&amp;lt;/font&amp;gt;&lt;/div&gt;</summary>
		<author><name>MassBot1</name></author>
	</entry>
</feed>