Collection Contents Index Bit data type Binary data types pdf/chap7.pdf

Reference Manual
   CHAPTER 7. SQL Data Types     

Date and time data types


Function 

For storing dates and times.

Top of page  Sending dates and times to the database

Dates and times may be sent to the database in one of the following ways:

When a time is sent to the database as a string (for the TIME data type) or as part of a string (for TIMESTAMP or DATE data types), the hours, minutes, and seconds must be separated by colons in the format hh:mm:ss.sss, but can appear anywhere in the string. The following are valid and unambiguous strings for specifying times:

21:35 -- 24 hour clock if no am or pm specified
10:00pm -- pm specified, so interpreted as 12 hour clock
10:00 -- 10:00am in the absence of pm
10:23:32.234 -- seconds and fractions of a second included

When a date is sent to the database as a string, conversion to a date is automatic. The string can be supplied in one of two ways:

Top of page  Unambiguous dates and times

Dates in the format yyyy/mm/dd or yyyy-mm-dd are always recognized unambiguously as dates, regardless of the DATE_ORDER setting. Other characters can be used as separators instead of "/" or "-"; for example, "?", a space character, or ",". You should use this format in any context where different users may be employing different DATE_ORDER settings. For example, in stored procedures, use of the unambiguous date format prevents misinterpretation of dates according to the user's DATE_ORDER setting.

Also, a string of the form hh:mm:ss.sss is interpreted unambiguously as a time.

For combinations of dates and times, any unambiguous date and any unambiguous time yield an unambiguous date-time value. Also, the form

YYYY-MM-DD HH.MM.SS.SSS

is an unambiguous date-time value. Periods can be used in the time only in combination with a date.

In other contexts, a more flexible date format can be used. Adaptive Server Anywhere can interpret a wide range of strings as dates. The interpretation depends on the setting of the database option DATE_ORDER. The DATE_ORDER database option can have the value MDY, YMD, or DMY (see SET OPTION statement). For example, the following statement sets the DATE_ORDER option to DMY:

SET OPTION DATE_ORDER = 'DMY' ;

The default DATE_ORDER setting is 'YMD'. The ODBC driver sets the DATE_ORDER option to 'YMD' whenever a connection is made. The value can still be changed using the SET TEMPORARY OPTION statement.

The database option DATE_ORDER determines whether the string 10/11/12 is interpreted by the database as Oct 11 1912, Nov 12 1910, or Nov 10 1912. The year, month, and day of a date string should be separated by some character (for example /, -, or space) and appear in the order specified by the DATE_ORDER option. The year can be supplied as either 2 or 4 digits, with 2 digit years defaulting to the 20th century. The month can be the name or number of the month. The hours and minutes are separated by a colon, but can appear anywhere in the string.

Notes 

Top of page  Retrieving dates and times from the database

Dates and times may be retrieved from the database in one of the following ways:

When a date or time is retrieved as a string, it is retrieved in the format specified by the database options DATE_FORMAT, TIME_FORMAT and TIMESTAMP_FORMAT. For descriptions of these options, see SET OPTION statement.

For Info     For information on functions that deals with dates and times, see Date and time functions. The following arithmetic operators are allowed on dates:

Top of page  Date and time comparisons

The DATE data type also contains a time. If the time is not specified when a date is entered into the database, the time defaults to 0:00 or 12:00am (midnight). Any date comparisons always involve the times as well. A database date value of '1992-05-23 10:00' will not be equal to the constant '1992-05-23'. The DATEFORMAT function or one of the other date functions can be used to compare parts of a date and time field. For example:

DATEFORMAT(invoice_date,'yyyy/mm/dd') = '1992/05/23'

If a database column requires only a date, client applications should ensure that times are not specified when data is entered into the database. This way, comparisons with date-only strings will work as expected.

If you wish to compare a date to a string as a string, you must use the DATEFORMAT function or CAST function to convert the date to a string before comparing.

Top of page  DATE data type [Date and Time]

Function 

A calendar date, such as a year, month and day.

Syntax 

DATETIME

Usage 

The year can be from the year 0001 to 9999. For historical reasons, a DATE column can also contain an hour and minute, but the TIMESTAMP data type is now recommended for anything with hours and minutes. A DATE value requires 4 bytes of storage.

Standards and compatibility 

See also 

DATETIME data type

SMALLDATETIME data type

Top of page  DATETIME data type [Date and Time]

Function 

A user-defined data type, implemented as TIMESTAMP.

Syntax 

SMALLDATETIME

Usage 

DATETIME is provided primarily for compatibility with Adaptive Server Enterprise.

Standards and compatibility 

See also 

DATE data type

SMALLDATETIME data type

Top of page  SMALLDATETIME data type [Date and Time]

Function 

A user-defined data type, implemented as TIMESTAMP.

Syntax 

SMALLDATETIME

Usage 

SMALLDATETIME is provided primarily for compatibility with Adaptive Server Enterprise.

Standards and compatibility 

See also 

DATE data type

DATETIME data type

Top of page  TIME data type [Date and Time]

Function 

The time of day, containing hour, minute, second and fraction of a second.

Syntax 

TIME

Usage 

The fraction is stored to 6 decimal places. A TIME value requires 8 bytes of storage. (ODBC standards restrict TIME data type to an accuracy of seconds. For this reason you should not use TIME data types in WHERE clause comparisons that rely on a higher accuracy than seconds.)

Standards and compatibility 

See also 

TIMESTAMP data type

Top of page  TIMESTAMP data type [Date and Time]

Function 

The point in time, containing year, month, day, hour, minute, second and fraction of a second.

Syntax 

TIMESTAMP

Usage 

The fraction is stored to 6 decimal places. A TIMESTAMP value requires 8 bytes of storage.

Although the range of possible dates for the TIMESTAMP data type is the same as the DATE type (covering years 0001 to 9999), the useful range of TIMESTAMP date types is from 1600-02-28 23:59:59 to 7911-01-01 00:00:00. Prior to, and after this range the time portion of the TIMESTAMP may be incomplete.

Standards and compatibility 

See also 

TIME data type

Top of page  

Collection Contents Index Bit data type Binary data types pdf/chap7.pdf