3.4. Data Types for Dates and Times
The DATE
, TIME
and TIMESTAMP
data types are used to work with data containing dates and times.
Dialect 3 supports all the three types, while Dialect 1 has only DATE
.
The DATE
type in Dialect 3 is date-only
, whereas the Dialect 1 DATE
type stores both date and time-of-day, equivalent to TIMESTAMP
in Dialect 3.
Dialect 1 has no date-only
type.
Dialect 1 DATE
data can be defined alternatively as TIMESTAMP
and this is recommended for new definitions in Dialect 1 databases.
Fractions of SecondsIf fractions of seconds are stored in date and time data types, Firebird stores them to ten-thousandths of a second. If a lower granularity is preferred, the fraction can be specified explicitly as thousandths, hundredths or tenths of a second in Dialect 3 databases of ODS 11 or higher.
The time-part of a TIME
or TIMESTAMP
is a 4-byte WORD, with room for decimilliseconds precision and time values are stored as the number of deci-milliseconds elapsed since midnight.
The actual precision of values stored in or read from time(stamp) functions and variables is:
CURRENT_TIME
defaults to seconds precision and can be specified up to milliseconds precision withCURRENT_TIME (0|1|2|3)
CURRENT_TIMESTAMP
milliseconds precision. Precision from seconds to milliseconds can be specified withCURRENT_TIMESTAMP (0|1|2|3)
Literal
'NOW'
: milliseconds precisionFunctions
DATEADD()
andDATEDIFF()
support up to milliseconds precision. Deci-milliseconds can be specified but they are rounded to the nearest integer before any operation is performedThe
EXTRACT()
function returns up to deci-milliseconds precision with theSECOND
andMILLISECOND
argumentsFor TIME and TIMESTAMP literals, Firebird happily accepts up to deci-milliseconds precision, but truncates (not rounds) the time part to the nearest lower or equal millisecond. Try, for example,
SELECT TIME '14:37:54.1249' FROM rdb$database
the
and+
operators work with deci-milliseconds precision, but only within the expression. As soon as something is stored or even just SELECTed from-
RDB$DATABASE
, it reverts to milliseconds precision
Deci-milliseconds precision is rare and is not currently stored in columns or variables.
The best assumption to make from all this is that, although Firebird stores TIME
and the TIMESTAMP
time-part values as the number of deci-milliseconds (10-4 seconds) elapsed since midnight, the actual precision could vary from seconds to milliseconds.
3.4.1. DATE
The DATE
data type in Dialect 3 stores only date without time.
The available range for storing data is from January 01, 1 to December 31, 9999.
Dialect 1 has no date-only
type.
In Dialect 1, date literals without a time part, as well as 'TODAY'
, 'YESTERDAY'
and 'TOMORROW'
automatically get a zero time part.
If, for some reason, it is important to you to store a Dialect 1 timestamp literal with an explicit zero time-part, the engine will accept a literal like '2016-12-25 00:00:00.0000'
.
However, '2016-12-25'
would have precisely the same effect, with fewer keystrokes!
3.4.2. TIME
The TIME
data type is available in Dialect 3 only.
It stores the time of day within the range from 00:00:00.0000 to 23:59:59.9999.
If you need to get the time-part from DATE
in Dialect 1, you can use the EXTRACT
function.
Examples Using EXTRACT()
|
EXTRACT (HOUR FROM DATE_FIELD)
|EXTRACT (MINUTE FROM DATE_FIELD)
|EXTRACT (SECOND FROM DATE_FIELD)
See also the EXTRACT()
function in the chapter entitled Built-in Functions.
3.4.3. TIMESTAMP
The TIMESTAMP
data type is available in Dialect 3 and Dialect 1.
It comprises two 32-bit words — a date-part and a time-part — to form a structure that stores both date and time-of-day.
It is the same as the DATE
type in Dialect 1.
The EXTRACT
function works equally well with TIMESTAMP
as with the Dialect 1 DATE
type.
3.4.4. Operations Using Date and Time Values
The method of storing date and time values makes it possible to involve them as operands in some arithmetic operations.
In storage, a date value or date-part of a timestamp is represented as the number of days elapsed since date zero
— November 17, 1898 — whilst a time value or the time-part of a timestamp is represented as the number of seconds (with fractions of seconds taken into account) since midnight.
An example is to subtract an earlier date, time or timestamp from a later one, resulting in an interval of time, in days and fractions of days.
Operand 1 | Operation | Operand 2 | Result |
---|---|---|---|
|
|
|
|
|
| Numeric value |
|
|
|
|
|
|
| Numeric value |
|
|
| Numeric value |
|
|
|
| Number of days elapsed, within the range |
|
| Numeric value |
|
|
|
| Number of seconds elapsed, within the range |
|
| Numeric value |
|
|
|
| Number of days and part-day, within the range |
|
| Numeric value |
|
The DATE
type is considered as TIMESTAMP
in Dialect 1.