<html>
<head>
    <base href="https://docs.jboss.org/author">
            <link rel="stylesheet" href="/author/s/en/2172/19/5/_/styles/combined.css?spaceKey=TEIID&amp;forWysiwyg=true" type="text/css">
    </head>
<body style="background: white;" bgcolor="white" class="email-body">
<div id="pageContent">
<div id="notificationFormat">
<div class="wiki-content">
<div class="email">
    <h2><a href="https://docs.jboss.org/author/display/TEIID/Date_Time+Functions">Date_Time Functions</a></h2>
    <h4>Page <b>edited</b> by             <a href="https://docs.jboss.org/author/display/~shawkins">Steven Hawkins</a>
    </h4>
        <br/>
                         <h4>Changes (9)</h4>
                                 
    
<div id="page-diffs">
                    <table class="diff" cellpadding="0" cellspacing="0">
    
            <tr><td class="diff-unchanged" >Date and time functions return or operate on dates, times, or timestamps. <br> <br></td></tr>
            <tr><td class="diff-changed-lines" >Parse and format Date/Time functions use the convention established within the java.text.SimpleDateFormat class to define the formats you can use with these functions. You can learn more about how this class defines formats by visiting the <span class="diff-deleted-words"style="color:#999;background-color:#fdd;text-decoration:line-through;">Sun Java Web site at the following [URL</span> <span class="diff-added-words"style="background-color: #dfd;">[Javadocs</span> for <span class="diff-deleted-words"style="color:#999;background-color:#fdd;text-decoration:line-through;">Sun Java|http://java.sun.com/javase/6/docs/api/java/text/SimpleDateFormat.html].</span> <span class="diff-added-words"style="background-color: #dfd;">SimpleDateFormat|http://docs.oracle.com/javase/6/docs/api/java/text/SimpleDateFormat.html].</span> <br></td></tr>
            <tr><td class="diff-unchanged" > <br>|| Function || Definition || Datatype Constraint || <br></td></tr>
            <tr><td class="diff-snipped" >...<br></td></tr>
            <tr><td class="diff-unchanged" >| SECOND\(x) | Return seconds | x in \{time, timestamp}, returns integer | <br>| TIMESTAMPCREATE(date, time) | Create a timestamp from a date and time | date in \{date}, time in \{time}, returns timestamp | <br></td></tr>
            <tr><td class="diff-deleted-lines" style="color:#999;background-color:#fdd;text-decoration:line-through;">| TIMESTAMPADD(interval, count, timestamp) | Add a specified interval (hour, day of week, month) to the timestamp, where intervals can have the following definition:# SQL_TSI_FRAC_SECOND - fractional seconds (billionths of a second) <br></td></tr>
            <tr><td class="diff-added-lines" style="background-color: #dfd;">| WEEK\(x) | Return week in year | x in \{date, timestamp}, returns integer | <br>| YEAR\(x) | Return four-digit year | x in \{date, timestamp}, returns integer | <br> <br>h1. Timestampadd/Timestampdiff <br> <br>h2. Timestampadd <br> <br>Add a specified interval amount to the timestamp. <br> <br>{code:SQL}TIMESTAMPADD(interval, count, timestamp){code} <br> <br>count is an integer and the return value is a timestamp. Intervals can be one of the following keywords:# SQL_TSI_FRAC_SECOND - fractional seconds (billionths of a second) <br></td></tr>
            <tr><td class="diff-unchanged" ># SQL_TSI_SECOND - seconds <br># SQL_TSI_MINUTE - minutes <br># SQL_TSI_HOUR - hours <br># SQL_TSI_DAY - days <br></td></tr>
            <tr><td class="diff-changed-lines" ># SQL_TSI_WEEK - weeks <span class="diff-added-words"style="background-color: #dfd;">using Sunday as the first day</span> <br></td></tr>
            <tr><td class="diff-unchanged" ># SQL_TSI_MONTH - months <br></td></tr>
            <tr><td class="diff-changed-lines" ># SQL_TSI_QUARTER - quarters (3 months) <span class="diff-added-words"style="background-color: #dfd;">where the first quarter is months 1-3, etc.</span> <br></td></tr>
            <tr><td class="diff-deleted-lines" style="color:#999;background-color:#fdd;text-decoration:line-through;"># SQL_TSI_YEAR - years | The interval constant may be specified either as a string literal or a constant value. Interval in \{string}, count in \{integer}, timestamp in \{date, time, timestamp} | <br>| TIMESTAMPDIFF(interval, startTime, endTime) | Calculate the approximate number of whole intervals in (endTime - startTime) using a specific interval type (as defined by the constants in TIMESTAMPADD). If (endTime &gt; startTime), a positive number will be returned. If (endTime &lt; startTime), a negative number will be returned. Calculations are approximate and may be less accurate over longer time spans. | Interval in \{string}; startTime, endTime in \{timestamp}, returns a long. | <br>| WEEK\(x) | Return week in year | x in \{date, timestamp}, returns integer | <br>| YEAR\(x) | Return four-digit year | x in \{date, timestamp}, returns integer | <br></td></tr>
            <tr><td class="diff-added-lines" style="background-color: #dfd;"># SQL_TSI_YEAR - years <br></td></tr>
            <tr><td class="diff-unchanged" > <br></td></tr>
            <tr><td class="diff-added-lines" style="background-color: #dfd;">The full interval amount based upon calendar fields will be added.  For example adding 1 QUARTER will move the timestamp up by three full months and not just to the start of the next calendar quarter. <br> <br>h2. Timestampdiff <br> <br>Calculates the number of date part intervals crossed between the two timestamps. <br> <br>{code}TIMESTAMPDIFF(interval, startTime, endTime){code} <br> <br>Interval can be one of the same keywords as used by timestampadd; startTime, endTime are timestamps and the return value is a long. <br> <br>If (endTime &gt; startTime), a non-negative number will be returned. If (endTime &lt; startTime), a non-positive number will be returned. The date part difference difference is counted regardless of how close the timestamps are.  For example, &#39;2000-01-02 00:00:00.0&#39; is still considered 1 hour ahead of &#39;2000-01-01 23:59:59.999999&#39;. <br> <br>{note:title=Compatibility Issues} <br>* Timestampdiff typically returns an integer, however Teiid&#39;s version returns a long.  You may receive an exception if you expect a value out of the integer range from a pushed down timestampdiff. <br>* Teiid&#39;s implementation of timestamp diff in 8.2 and prior versions returned values based upon the number of whole canonical interval approximations (365 days in a year, 91 days in a quarter, 30 days in a month, etc.) crossed.  For example the difference in months between 2013-03-24 and 2013-04-01 was 0, but based upon the date parts crossed is 1.  See [TEIID:System Properties] for backwards compatibility. <br>{note} <br> <br></td></tr>
            <tr><td class="diff-unchanged" >h1. Parsing Date Datatypes from Strings <br> <br></td></tr>
            <tr><td class="diff-changed-lines" >Teiid does not implicitly convert strings that contain dates presented in different formats, such as ‘19970101’ and ‘31/1/1996’ to date-related datatypes. You can, however, use the parseDate, parseTime, and parseTimestamp functions, described in the next section, to explicitly convert strings with a different format to the appropriate datatype. These functions use the convention established within the java.text.SimpleDateFormat class to define the formats you can use with these functions. You can learn more about how this class defines date and time string formats by visiting the <span class="diff-deleted-words"style="color:#999;background-color:#fdd;text-decoration:line-through;">[Sun Java Web site|http://java.sun.com/j2se/1.4.2/docs/api/java/text/SimpleDateFormat.html].</span> <span class="diff-added-words"style="background-color: #dfd;">[Javadocs for SimpleDateFormat|http://docs.oracle.com/javase/6/docs/api/java/text/SimpleDateFormat.html].</span>  Note that the format strings will be locale specific to your Java default locale. <br></td></tr>
            <tr><td class="diff-unchanged" > <br>For example, you could use these function calls, with the formatting string that adheres to the java.text.SimpleDateFormat convention, to parse strings and return the datatype you need: <br></td></tr>
            <tr><td class="diff-snipped" >...<br></td></tr>
    
            </table>
    </div>                            <h4>Full Content</h4>
                    <div class="notificationGreySide">
        <p>Date and time functions return or operate on dates, times, or timestamps.</p>

<p>Parse and format Date/Time functions use the convention established within the java.text.SimpleDateFormat class to define the formats you can use with these functions. You can learn more about how this class defines formats by visiting the <a href="http://docs.oracle.com/javase/6/docs/api/java/text/SimpleDateFormat.html" class="external-link" rel="nofollow">Javadocs for SimpleDateFormat</a>.</p>

<div class='table-wrap'>
<table class='confluenceTable'><tbody>
<tr>
<th class='confluenceTh'> Function </th>
<th class='confluenceTh'> Definition </th>
<th class='confluenceTh'> Datatype Constraint </th>
</tr>
<tr>
<td class='confluenceTd'> CURDATE() </td>
<td class='confluenceTd'> Return current date </td>
<td class='confluenceTd'> returns date </td>
</tr>
<tr>
<td class='confluenceTd'> CURTIME() </td>
<td class='confluenceTd'> Return current time </td>
<td class='confluenceTd'> returns time </td>
</tr>
<tr>
<td class='confluenceTd'> NOW() </td>
<td class='confluenceTd'> Return current timestamp (date and time) </td>
<td class='confluenceTd'> returns timestamp </td>
</tr>
<tr>
<td class='confluenceTd'> DAYNAME&#40;x) </td>
<td class='confluenceTd'> Return name of day in the default locale </td>
<td class='confluenceTd'> x in {date, timestamp}, returns string </td>
</tr>
<tr>
<td class='confluenceTd'> DAYOFMONTH&#40;x) </td>
<td class='confluenceTd'> Return day of month </td>
<td class='confluenceTd'> x in {date, timestamp}, returns integer </td>
</tr>
<tr>
<td class='confluenceTd'> DAYOFWEEK&#40;x) </td>
<td class='confluenceTd'> Return day of week (Sunday=1, see also <a href="/author/display/TEIID/System+Properties" title="System Properties">System Properties</a> for customization) </td>
<td class='confluenceTd'> x in {date, timestamp}, returns integer </td>
</tr>
<tr>
<td class='confluenceTd'> DAYOFYEAR&#40;x) </td>
<td class='confluenceTd'> Return Julian day number </td>
<td class='confluenceTd'> x in {date, timestamp}, returns integer </td>
</tr>
<tr>
<td class='confluenceTd'> EXTRACT(YEAR&#124;MONTH&#124;DAY&#124;HOUR&#124;MINUTE&#124;SECOND FROM x) </td>
<td class='confluenceTd'> Return the given field value from the date value x. Produces the same result as the assoceated YEAR, MONTH, DAYOFMONTH, HOUR, MINUTE, SECOND functions.The SQL specification also allows for TIMEZONE_HOUR and TIMEZONE_MINUTE as extraction targets. In Teiid all date values are in the timezone of the server. </td>
<td class='confluenceTd'> x in {date, time, timestamp}, returns integer </td>
</tr>
<tr>
<td class='confluenceTd'> FORMATDATE(x, y) </td>
<td class='confluenceTd'> Format date x using format y </td>
<td class='confluenceTd'> x is date, y is string, returns string </td>
</tr>
<tr>
<td class='confluenceTd'> FORMATTIME(x, y) </td>
<td class='confluenceTd'> Format time x using format y </td>
<td class='confluenceTd'> x is time, y is string, returns string </td>
</tr>
<tr>
<td class='confluenceTd'> FORMATTIMESTAMP(x, y) </td>
<td class='confluenceTd'> Format timestamp x using format y </td>
<td class='confluenceTd'> x is timestamp, y is string, returns string </td>
</tr>
<tr>
<td class='confluenceTd'> FROM_UNIXTIME (unix_timestamp) </td>
<td class='confluenceTd'> Return the Unix timestamp (in seconds) as a Timestamp value </td>
<td class='confluenceTd'> Unix timestamp (in seconds) </td>
</tr>
<tr>
<td class='confluenceTd'> HOUR&#40;x) </td>
<td class='confluenceTd'> Return hour (in military 24-hour format) </td>
<td class='confluenceTd'> x in {time, timestamp}, returns integer </td>
</tr>
<tr>
<td class='confluenceTd'> MINUTE&#40;x) </td>
<td class='confluenceTd'> Return minute </td>
<td class='confluenceTd'> x in {time, timestamp}, returns integer </td>
</tr>
<tr>
<td class='confluenceTd'> MODIFYTIMEZONE (timestamp, startTimeZone, endTimeZone) </td>
<td class='confluenceTd'> Returns a timestamp based upon the incoming timestamp adjusted for the differential between the start and end time zones.&nbsp; i.e. if the server is in GMT-6, then modifytimezone({ts '2006-01-10 04:00:00.0'},'GMT-7', 'GMT-8') will return the timestamp {ts '2006-01-10 05:00:00.0'} as read in GMT-6.&nbsp; The value has been adjusted 1 hour ahead to compensate for the difference between GMT-7 and GMT-8. </td>
<td class='confluenceTd'> startTimeZone and endTimeZone are strings, returns a timestamp </td>
</tr>
<tr>
<td class='confluenceTd'> MODIFYTIMEZONE (timestamp, endTimeZone) </td>
<td class='confluenceTd'> Return a timestamp in the same manner as modifytimezone(timestamp, startTimeZone, endTimeZone), but will assume that the startTimeZone is the same as the server process. </td>
<td class='confluenceTd'> Timestamp is a timestamp; endTimeZone is a string, returns a timestamp </td>
</tr>
<tr>
<td class='confluenceTd'> MONTH&#40;x) </td>
<td class='confluenceTd'> Return month </td>
<td class='confluenceTd'> x in {date, timestamp}, returns integer </td>
</tr>
<tr>
<td class='confluenceTd'> MONTHNAME&#40;x) </td>
<td class='confluenceTd'> Return name of month in the default locale </td>
<td class='confluenceTd'> x in {date, timestamp}, returns string </td>
</tr>
<tr>
<td class='confluenceTd'> PARSEDATE(x, y) </td>
<td class='confluenceTd'> Parse date from x using format y </td>
<td class='confluenceTd'> x, y in {string}, returns date </td>
</tr>
<tr>
<td class='confluenceTd'> PARSETIME(x, y) </td>
<td class='confluenceTd'> Parse time from x using format y </td>
<td class='confluenceTd'> x, y in {string}, returns time </td>
</tr>
<tr>
<td class='confluenceTd'> PARSETIMESTAMP(x,y) </td>
<td class='confluenceTd'> Parse timestamp from x using format y </td>
<td class='confluenceTd'> x, y in {string}, returns timestamp </td>
</tr>
<tr>
<td class='confluenceTd'> QUARTER&#40;x) </td>
<td class='confluenceTd'> Return quarter </td>
<td class='confluenceTd'> x in {date, timestamp}, returns integer </td>
</tr>
<tr>
<td class='confluenceTd'> SECOND&#40;x) </td>
<td class='confluenceTd'> Return seconds </td>
<td class='confluenceTd'> x in {time, timestamp}, returns integer </td>
</tr>
<tr>
<td class='confluenceTd'> TIMESTAMPCREATE(date, time) </td>
<td class='confluenceTd'> Create a timestamp from a date and time </td>
<td class='confluenceTd'> date in {date}, time in {time}, returns timestamp </td>
</tr>
<tr>
<td class='confluenceTd'> WEEK&#40;x) </td>
<td class='confluenceTd'> Return week in year </td>
<td class='confluenceTd'> x in {date, timestamp}, returns integer </td>
</tr>
<tr>
<td class='confluenceTd'> YEAR&#40;x) </td>
<td class='confluenceTd'> Return four-digit year </td>
<td class='confluenceTd'> x in {date, timestamp}, returns integer </td>
</tr>
</tbody></table>
</div>


<h1><a name="Date_TimeFunctions-Timestampadd%2FTimestampdiff"></a>Timestampadd/Timestampdiff</h1>

<h2><a name="Date_TimeFunctions-Timestampadd"></a>Timestampadd</h2>

<p>Add a specified interval amount to the timestamp.</p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="theme: Confluence; brush: sql; gutter: false" style="font-size:12px; font-family: ConfluenceInstalledFont,monospace;">TIMESTAMPADD(interval, count, timestamp)</pre>
</div></div>

<p>count is an integer and the return value is a timestamp. Intervals can be one of the following keywords:# SQL_TSI_FRAC_SECOND - fractional seconds (billionths of a second)</p>
<ol>
        <li>SQL_TSI_SECOND - seconds</li>
        <li>SQL_TSI_MINUTE - minutes</li>
        <li>SQL_TSI_HOUR - hours</li>
        <li>SQL_TSI_DAY - days</li>
        <li>SQL_TSI_WEEK - weeks using Sunday as the first day</li>
        <li>SQL_TSI_MONTH - months</li>
        <li>SQL_TSI_QUARTER - quarters (3 months) where the first quarter is months 1-3, etc.</li>
        <li>SQL_TSI_YEAR - years</li>
</ol>


<p>The full interval amount based upon calendar fields will be added.  For example adding 1 QUARTER will move the timestamp up by three full months and not just to the start of the next calendar quarter.</p>

<h2><a name="Date_TimeFunctions-Timestampdiff"></a>Timestampdiff</h2>

<p>Calculates the number of date part intervals crossed between the two timestamps.</p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="theme: Confluence; brush: java; gutter: false" style="font-size:12px; font-family: ConfluenceInstalledFont,monospace;">TIMESTAMPDIFF(interval, startTime, endTime)</pre>
</div></div>

<p>Interval can be one of the same keywords as used by timestampadd; startTime, endTime are timestamps and the return value is a long.</p>

<p>If (endTime &gt; startTime), a non-negative number will be returned. If (endTime &lt; startTime), a non-positive number will be returned. The date part difference difference is counted regardless of how close the timestamps are.  For example, '2000-01-02 00:00:00.0' is still considered 1 hour ahead of '2000-01-01 23:59:59.999999'.</p>

<div class='panelMacro'><table class='noteMacro'><colgroup><col width='24'><col></colgroup><tr><td valign='top'><img src="/author/images/icons/emoticons/warning.gif" width="16" height="16" align="absmiddle" alt="" border="0"></td><td><b>Compatibility Issues</b><br /><ul>
        <li>Timestampdiff typically returns an integer, however Teiid's version returns a long.  You may receive an exception if you expect a value out of the integer range from a pushed down timestampdiff.</li>
        <li>Teiid's implementation of timestamp diff in 8.2 and prior versions returned values based upon the number of whole canonical interval approximations (365 days in a year, 91 days in a quarter, 30 days in a month, etc.) crossed.  For example the difference in months between 2013-03-24 and 2013-04-01 was 0, but based upon the date parts crossed is 1.  See <a href="/author/display/TEIID/System+Properties" title="System Properties">System Properties</a> for backwards compatibility.</li>
</ul>
</td></tr></table></div>

<h1><a name="Date_TimeFunctions-ParsingDateDatatypesfromStrings"></a>Parsing Date Datatypes from Strings</h1>

<p>Teiid does not implicitly convert strings that contain dates presented in different formats, such as ‘19970101’ and ‘31/1/1996’ to date-related datatypes. You can, however, use the parseDate, parseTime, and parseTimestamp functions, described in the next section, to explicitly convert strings with a different format to the appropriate datatype. These functions use the convention established within the java.text.SimpleDateFormat class to define the formats you can use with these functions. You can learn more about how this class defines date and time string formats by visiting the <a href="http://docs.oracle.com/javase/6/docs/api/java/text/SimpleDateFormat.html" class="external-link" rel="nofollow">Javadocs for SimpleDateFormat</a>.  Note that the format strings will be locale specific to your Java default locale.</p>

<p>For example, you could use these function calls, with the formatting string that adheres to the java.text.SimpleDateFormat convention, to parse strings and return the datatype you need:</p>

<div class='table-wrap'>
<table class='confluenceTable'><tbody>
<tr>
<th class='confluenceTh'> String </th>
<th class='confluenceTh'> Function Call To Parse String </th>
</tr>
<tr>
<td class='confluenceTd'> '1997010' </td>
<td class='confluenceTd'> parseDate(myDateString, 'yyyyMMdd') </td>
</tr>
<tr>
<td class='confluenceTd'> '31/1/1996' </td>
<td class='confluenceTd'> parseDate(myDateString, 'dd''/''MM''/''yyyy') </td>
</tr>
<tr>
<td class='confluenceTd'> '22:08:56 CST' </td>
<td class='confluenceTd'> parseTime (myTime, 'HH:mm:ss z') </td>
</tr>
<tr>
<td class='confluenceTd'> '03.24.2003 at 06:14:32' </td>
<td class='confluenceTd'> parseTimestamp(myTimestamp, 'MM.dd.yyyy ''at'' hh:mm:ss') </td>
</tr>
</tbody></table>
</div>


<h1><a name="Date_TimeFunctions-SpecifyingTimeZones"></a>Specifying Time Zones</h1>

<p>Time zones can be specified in several formats. Common abbreviations such as EST for "Eastern Standard Time" are allowed but discouraged, as they can be ambiguous. Unambiguous time zones are defined in the form continent or ocean/largest city. For example, America/New_York, America/Buenos_Aires, or Europe/London. Additionally, you can specify a custom time zone by GMT offset: GMT[+/-]HH:MM.</p>

<p>For example: GMT-05:00</p>
    </div>
        <div id="commentsSection" class="wiki-content pageSection">
        <div style="float: right;" class="grey">
                        <a href="https://docs.jboss.org/author/users/removespacenotification.action?spaceKey=TEIID">Stop watching space</a>
            <span style="padding: 0px 5px;">|</span>
                <a href="https://docs.jboss.org/author/users/editmyemailsettings.action">Change email notification preferences</a>
</div>
        <a href="https://docs.jboss.org/author/display/TEIID/Date_Time+Functions">View Online</a>
        |
        <a href="https://docs.jboss.org/author/pages/diffpagesbyversion.action?pageId=18646253&revisedVersion=5&originalVersion=4">View Changes</a>
                |
        <a href="https://docs.jboss.org/author/display/TEIID/Date_Time+Functions?showComments=true&amp;showCommentArea=true#addcomment">Add Comment</a>
            </div>
</div>
</div>
</div>
</div>
</body>
</html>