简体   繁体   中英

finding current server time in DB2 in specific format

I want to fetch current server time in DB2 in the format HH:MM:SS:M

eg 21:05:38:7

How should I modify below query to achieve it:

SELECT current time FROM sysibm.sysdummy1

Thanks for reading!

Times, Dates, Timestamps are all data types that DB2 stores in some internal data representation form. It is only when you ask to see it, SELECT MyTime FROM MyTable, that DB2 is forced to convert those bytes into something a human can read. DB2 will use the locale settings to convert a DATE to eg 2012-03-17, 17/03/2012, ...

This can be great fun when you develop on a central DB2 database and your customers are from the UK, the USA, France, India, .... They will all complain that the dates are malformed. You should always think DB2 will do it wrong and do the formatting yourself.

These things are best done by the reporting tool you use. So you can use the right format for your user and you don't have to settle for whatever the locale settings on the DB2 database server are.

In some cases you will have to do the formatting in SQL. Have you read the article Amit Bhargava referred to ? Near the end the author creates a function ts_fmt that can handle a few formats and that can easily be extended with whatever formatting you need: Just add this code right before the ELSE part.

when 'HH:MM:SS:M' 
    then hh || ':' || mi || ':' || ss || ':' || nnnnnn

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM