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

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.

NickLitten


IBM i Software Developer, Digital Dad, AS400 Anarchist, RPG Modernizer, Shameless Trekkie, Belligerent Nerd, Englishman Abroad and Passionate Eater of Cheese and Biscuits.

Nick Litten Dot Com is a mixture of blog posts that can be sometimes serious, frequently playful and probably down-right pointless all in the space of a day.

Enjoy your stay, feel free to comment and remember: If at first you don't succeed then skydiving probably isn't a hobby you should look into.

Nick Litten

related posts:

  • 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"}

    Subscribe NOW
    7-day free trial

    Take This Course with ALL ACCESS

    Unlock your Learning Potential with instant access to every course and all new courses as they are released.
     [ For Serious Software Developers only ]

    Online Learning for IBM i Software Technology Professionals

    “The more that you read, the more things you will know. The more that you learn, the more places you’ll go.” – Dr. Seuss

    >