DB2- Date, Time, Timestamp

The basics -To get the current date, time, and timestamp using SQL, reference the appropriate DB2 registers.


Using Date, Time in DB2 SQL Queries you need

FROM   sysibm.sysdummy1;
FROM   sysibm.sysdummy1;
FROM   sysibm.sysdummy1;

The sysibm.sysdummy1 table is a special in-memory table that can be used to discover the value of DB2 registers as illustrated above.

You can also use the VALUES keyword to evaluate the register or expression.

Current Date, Time and Timestamp

For example, from the DB2 Command Line Processor (CLP), the following SQL statements reveal similar information:

  • VALUES CURRENT timestamp

For the remaining examples, I will simply provide the function or expression without repeating SELECT ... FROM sysibm.sysdummy1 or using the VALUES clause.

To get the current time or current timestamp adjusted to GMT/CUT, subtract the current timezone register from the current time or timestamp:

CURRENT time - CURRENT timezone
CURRENT timestamp - CURRENT timezone

Given a date, time, or timestamp, you can extract (where applicable) the year, month, day, hour, minutes, seconds, and microseconds portions independently using the appropriate function:

  • YEAR (CURRENT timestamp)
  • Month (CURRENT timestamp)
  • DAY(CURRENT timestamp)
  • Hour (CURRENT timestamp)
  • Minute (CURRENT timestamp)
  • SECOND (current timestamp)
  • MICROSECOND (current timestamp)

Extracting the date and time independently from a timestamp is also very easy:

  • DATE (CURRENT timestamp)
  • Time (CURRENT timestamp)

You can also perform date and time calculations using, for lack of a better term, English:

  • CURRENT date + 1 year
  • CURRENT date + 3 years + 2 months + 15 days
  • CURRENT time + 5 hours  3 minutes + 10 seconds

To calculate how many days there are between two dates, you can subtract dates as in the following:

days (current date) - days (date('1999-10-22'))

And here is an example of how to get the current timestamp with the microseconds portion reset to zero:


If you want to concatenate date or time values with other text, you need to convert the value into a character string first. To do this, you can simply use the CHAR() function:

  • CHAR(CURRENT date)
  • char(CURRENT time)
  • char(CURRENT date + 12 hours)

To convert a character string to a date or time value, you can use:

TIMESTAMP ('2002-10-20-')
Timestamp ('2002-10-20 12:00:00'
Date ('2002-10-20')
Date ('10/20/2002')
Time ('12:00:00')
Time ('12.00.00')

The TIMESTAMP(), DATE() and TIME() functions accept several more formats. The above list are examples only. There are many more to try.

Keep Reading

Author: Srini

Experienced software developer. Skills in Development, Coding, Testing and Debugging. Good Data analytic skills (Data Warehousing and BI). Also skills in Mainframe.

6 thoughts

Comments are closed.