简体   繁体   English

以特定格式在DB2中查找当前服务器时间

[英]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 我想以HH:MM:SS:M的格式获取DB2中的当前服务器时间。

eg 21:05:38:7 例如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. 时间,日期和时间戳是DB2以某种内部数据表示形式存储的所有数据类型。 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. 只有当您要求从MyTable中选择SELECT MyTime时,DB2才被迫将这些字节转换为人类可以读取的内容。 DB2 will use the locale settings to convert a DATE to eg 2012-03-17, 17/03/2012, ... DB2将使用语言环境设置将DATE转换为例如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. 当您在中央DB2数据库上进行开发并且您的客户来自英国,美国,法国,印度...时,这可能会很有趣。他们都会抱怨日期不正确。 You should always think DB2 will do it wrong and do the formatting yourself. 您应该始终认为DB2会做错事情,并且自己进行格式化。

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. 因此,您可以为用户使用正确的格式,而不必满足DB2数据库服务器上的任何语言环境设置。

In some cases you will have to do the formatting in SQL. 在某些情况下,您将必须使用SQL进行格式化。 Have you read the article Amit Bhargava referred to ? 您是否已阅读Amit Bhargava提到的文章 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. 在结尾处,作者创建了一个ts_fmt函数,该函数可以处理几种格式,并且可以轻松扩展为所需的任何格式:只需在ELSE部分之前添加此代码即可。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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