InterSystems IRIS Data Platform 2019.2  /  InterSystems SQL Reference  /  SQL Functions

InterSystems SQL Reference
Previous section           Next section
InterSystems: The power behind what matters   

A date function that converts a formatted string to a timestamp.
date_string A string expression to be converted to a timestamp. This expression may contain a date value, a time value, or a date and time value.
format Optional — A date and time format string corresponding to date_string. If omitted, defaults to DD MON YYYY HH:MI:SS.
The TO_TIMESTAMP function converts date and time strings in various formats to a standard timestamp, with data type TIMESTAMP. TO_TIMESTAMP returns a timestamp with the following format:
yyyy-mm-dd hh:mm:ss
with leading zeroes always included. Time is specified using a 24–hour clock. By default, a returned timestamp does not include fractional seconds.
TO_TIMESTAMP returns a standard timestamp in ODBC format. TO_POSIXTIME returns an encoded 64–bit timestamp. TO_POSIXTIME is the recommended timestamp format for new programming.
If date_string omits components of the timestamp, TO_TIMESTAMP supplies the missing components. If both date_string and format omit the year, yyyy defaults to the current year; if only date_string omits the year, it defaults to 00, which is expanded to a four-digit year according to the year format element. If a day or month value is omitted, dd defaults to 01; mm-dd defaults to 01-01. Therefore, if both date_string and format omit if the date component of a timestamp, TO_TIMESTAMP defaults to January 1 of the current year, in ODBC format: yyyy-01-01.
A missing time component defaults to 00. Fractional seconds are supported, but must be explicitly specified; no fractional seconds are provided by default.
TO_TIMESTAMP supports conversion of two-digit years to four digits. TO_TIMESTAMP supports conversion of 12-hour clock time to 24-hour clock time. It provides range validation of date and time element values, including leap year validation. Range validation violations generate an SQLCODE -400 error.
This function can also be invoked from ObjectScript using the TOTIMESTAMP() method call:
The TO_TIMESTAMP function can be used in data definition when supplying a default value to a timestamp field. For example:
CREATE TABLE Sample.MyEmpReviews
In this example, the user inserting a record can either suppler a ReviewDate value, supply no ReviewDate value and get the default timestamp of the 365th day of the current year, or supply a ReviewDate of NULL and get NULL.
TO_TIMESTAMP can be used with the CREATE TABLE or ALTER TABLE ADD COLUMN statements. Only a literal value for date_string can be used in this context. For further details, refer to the CREATE TABLE command.
Related SQL Functions
Date and Time String
The date_string argument specifies a date and time string literal. If you supply a date string with no time component, TO_TIMESTAMP supplies the time value 00:00:00. If you supply a time string with no date component, TO_TIMESTAMP supplies the date of 01–01 (January 1) of the current year.
You can supply a date and time string of any kind for the input date_string. Each date_string character must correspond to the format string, with the following exceptions:
A format is a string of one or more format elements specified according to the following rules:
Format Elements
The following table lists the valid date format elements for the format argument:
Element Meaning
DD Two-digit day of month (01-31). Leading zeros are not required, unless format contains no date separator characters.
Two-digit month number (01-12; 01 = January). Leading zeros are not required, unless format contains no date separator characters.
In Japanese and Chinese, a month number consists of a numeric value followed by the ideogram for “month”.
MON Abbreviated name of month, as specified by the MonthAbbr property in the current locale. By default, in English this is the first three letters of the month name. In other locales, month abbreviations may be more than three letters long and/or may not consist of the first letters of the month name. A period character is not permitted. Not case-sensitive.
MONTH Full name of the month, as specified by the MonthName property in the current locale. Not case-sensitive.
YYYY Four-digit year.
YY Last two digits of the year. The first 2 digits of a YY 2-digit year default to 19.
RR / RRRR Two-digit year to four-digit year conversion. (See below.)
DDD Day of the year. The number of days since January 1. (See below.)
HH Hour, specified as either 01–12 or 00–23, depending on whether a meridian indicator (AM or PM) is specified. Can be specified as HH12 or HH24.
MI Minute, specified as 00–59.
SS Second, specified as 00–59.
FF Fractions of a second. The two-letter element FF indicates that one or more fractional digits are returned. The actual number of fractional digits returned is specified in date_string, which can specify any number of fractional digits. TO_TIMESTAMP returns exactly the fractional value explicitly supplied in date_string; trailing zeroes are neither padded nor truncated. You must provide a decimal separator format character. Fractional seconds are ignored if this format element is not the two-letter element FF. By default, an InterSystems IRIS timestamp does not include fractional seconds.
AM / PM Meridian indicator, specifies a 12–hour clock. (See below.)
A.M. / P.M. Meridian indicator (with periods), specifies a 12–hour clock. (See below.)
A TO_TIMESTAMP format can also include a D (day of week number), DY (day of week abbreviation), or DAY (day of week name) element to match the input date_string. However, these format elements are not validated or used to determine the return value. For further details on these format elements, refer to TO_CHAR.
Two-Digit Year Conversion (RR and RRRR formats)
The RR format provides two-digit to four-digit year conversion. This conversion is based on the current year. If the current year is in the first half of a century (for example, 2000 through 2050), two-digit years from 00 through 49 are expanded to a four-digit year in the current century, and two-digit years from 50 through 99 are expanded to a four-digit year in the previous century. If the current year is in the second half of a century (for example, 2050 through 2099), all two-digit years are expanded to a four-digit year in the current century. This expansion of two-digit years to four-digit years is shown in the following example:
       TO_TIMESTAMP('29 September 18','DD MONTH RR'),
       TO_TIMESTAMP('29 September 49','DD MONTH RR'),
       TO_TIMESTAMP('29 September 50','DD MONTH RR'),
       TO_TIMESTAMP('29 September 77','DD MONTH RR')
The RRRR format permits you to input a mix of two–digit and four-digit years. Four-digit years are passed through unchanged (the same as YYYY). Two-digit years are converted to four-digit years, using the RR format algorithm. This is shown in the following example:
SELECT TO_TIMESTAMP('29 September 2018','DD MONTH RRRR')AS FourDigit,
       TO_TIMESTAMP('29 September 18','DD MONTH RRRR') AS TwoDigit,
       TO_TIMESTAMP('29 September 1949','DD MONTH RRRR') AS FourDigit,
       TO_TIMESTAMP('29 September 49','DD MONTH RRRR') AS TwoDigit,
       TO_TIMESTAMP('29 September 1950','DD MONTH RRRR') AS FourDigit,
       TO_TIMESTAMP('29 September 50','DD MONTH RRRR') AS TwoDigit
Day of the Year (DDD format)
You can use DDD to convert the day of the year (number of days elapsed since January 1) to an actual date. The format string DDD YYYY must be paired with a corresponding date_string consisting of an integer number of days and a four-digit year. (Two-digit years must be specified as RR (not YY) when used with DDD.) The format string DDD defaults to the current year. The number of elapsed days must be a positive integer in the range 1 through 365 (366 if YYYY is a leap year). The four-digit year must be within the standard InterSystems IRIS date range: 1841 through 9999. (If you omit the year, it defaults to the current year.) The DDD and year (YYYY, RRRR, or RR) format elements can be specified in any order; a separator character between them is mandatory; this separator can be a blank space. The following example shows this use of Day of the Year:
If a format string contains both a DD and a DDD element, the DDD element is dominant. This is shown in the following example, which returns 2008-02-29 00:00:00 (not 2008-12-31 00:00:00):
TO_TIMESTAMP permits you to return a date expression corresponding to a day of the year. TO_CHAR permits you to return the day of the year corresponding to a date expression.
Dates Before 1841
TO_TIMESTAMP cannot represent a date before December 31, 1840. Attempted to input such a date results in an SQLCODE -400 error.
TO_POSIXTIME can represent dates back to January 1, 0001. It can include time values with up to 6 digits of fractional seconds precision.
TO_DATE provides a Julian date format than can represent dates back to January 1, 4712 BCE. Julian date conversion converts a seven-digit internal positive integer value (a Julian day count) to a display-format or ODBC-format date. Time values are not supported for Julian dates.
12-Hour Clock Time
A TIMESTAMP always represents time using a 24-hour clock. A date_string may represent time using a 12-hour clock or a 24-hour clock. TO_TIMESTAMP assumes a 24-hour clock, unless one of the following applies:
The following embedded SQL example specifies date strings in various formats. The first one uses the default format, the others specify a format. All of these convert date_string to the timestamp value of 2018–06–29 00:00:00:
       TO_TIMESTAMP('29 JUN 2018'),
       TO_TIMESTAMP('2018 Jun 29','YYYY MON DD'),
       TO_TIMESTAMP('JUNE 29, 2018','month dd, YYYY'),
    INTO :a,:b,:c,:d,:e,:f)
  IF SQLCODE=0 { WRITE !,a,!,b,!,c,!,d,!,e,!,f }
The following example specifies the YYYYMM date format. It does not require element separators. TO_TIMESTAMP supplies the missing day and time values:
This example returns the timestamp 2018–06–01 00:00:00.
The following example specifies just the HH:MI:SS.FF time format. TO_TIMESTAMP supplies the missing date value. In each case, this example returns the date of 2018–01–01 (where 2018 is the current year):
Note that fractional seconds are passed through exactly as specified, with no padding or truncation.
The following example shows some other ways to specify a time format with fractional seconds:
All three invocations of TO_TIMESTAMP return an ODBC-format timestamp with the time portion value as 11:34:22.9678. For the first two, the omitted date portion defaults to January 1 of the current year; the third supplies a date portion value.
See Also

Previous section           Next section
Send us comments on this page
View this book as PDF   |  Download all PDFs
Copyright © 1997-2019 InterSystems Corporation, Cambridge, MA
Content Date/Time: 2019-09-19 06:56:35