How to initialize a timestamp variable using IBM i SQL

  • Home
  • /
  • Blog
  • /
  • How to initialize a timestamp variable using IBM i SQL

April 24, 2024

How to initialize a timestamp variable using IBM i SQL

By NickLitten

April 24, 2024

AI Generated Answers

More SQL Basics from the AI Archives

How to initialize a timestamp variable using ibm i sql

To initialize a timestamp in IBM i SQL, you can use the CURRENT TIMESTAMP special register which specifies a timestamp based on the reading of the time-of-day clock when the SQL statement is executed.

If you need a timestamp with a specific precision, you can reference it as CURRENT TIMESTAMP(integer), where integer can range from 0 to 12, with the default precision being 6.

Here’s an example of how you might use it in an SQL statement:

INSERT INTO your_table_name
VALUES (CURRENT TIMESTAMP, :column2, :column3, ...)

This will insert a new row into your_table_name with the current timestamp and the values for the other columns provided by the host variables or literals [more]

Additionally, if you want to combine date and time to create a timestamp, you can use the %DATE and %TIME built-in functions like this:

timestamp = %date(numdate : *ymd) + %time(numtime : *iso);

This will create a timestamp by adding the date and time together [more].

Remember to replace numdate and numtime with your actual numeric date and time values.

  • Why would you concatenate date and time if you can just use the build in function %Timestamp ? (ie. timestamp = %Timestamp)

    Concatenating these can have a potential issue on end of day that would result in a date from yesterday with a time of today.

  • I m new on ibm i i have a question, (sorrry for my english i m french), i have a a TS collumn in my table can i fill this TS whit the current date, by using strsql ?
    Like : ‘insert into table (‘TS FIELD’) values (%timestamp)?

    Thx

  • {"email":"Email address invalid","url":"Website address invalid","required":"Required field missing"}

    Join the IBM i Community for FREE Presentations, Lessons, Hints and Tips

    >