简体   繁体   English

DB2日期格式

[英]DB2 Date format

I just want to format current date into yyyymmdd in DB2. 我只想在DB2中将当前日期格式化为yyyymmdd

I see the date formats available, but how can I use them? 我看到可用的日期格式,但是如何使用它们呢?

http://publib.boulder.ibm.com/infocenter/dzichelp/v2r2/index.jsp?topic=%2Fcom.ibm.db2z10.doc.intro%2Fsrc%2Ftpc%2Fdb2z_datetimetimestamp.htm http://publib.boulder.ibm.com/infocenter/dzichelp/v2r2/index.jsp?topic=%2Fcom.ibm.db2z10.doc.intro%2Fsrc%2Ftpc%2Fdb2z_datetimetimestamp.htm

SELECT CURDATE() FROM SYSIBM.SYSDUMMY1;

I dont see any straightforward way to use the above listed formats. 我看不到使用以上列出的格式的任何直接方法。

Any suggestion? 有什么建议吗?

SELECT VARCHAR_FORMAT(CURRENT TIMESTAMP, 'YYYYMMDD')
FROM SYSIBM.SYSDUMMY1

Should work on both Mainframe and Linux/Unix/Windows DB2. 应该可以在大型机和Linux / Unix / Windows DB2上使用。 Info Center entry for VARCHAR_FORMAT() . VARCHAR_FORMAT()信息中心条目

另一种解决方案REPLACE(CHAR(current date,ISO),'-','')

This isn't straightforward, but 这并不简单,但是

SELECT CHAR(CURRENT DATE, ISO) FROM SYSIBM.SYSDUMMY1

returns the current date in yyyy-mm-dd format. 以yyyy-mm-dd格式返回当前日期。 You would have to substring and concatenate the result to get yyyymmdd. 您必须将其字符串化并连接结果才能得到yyyymmdd。

SELECT SUBSTR(CHAR(CURRENT DATE, ISO), 1, 4) ||
    SUBSTR(CHAR(CURRENT DATE, ISO), 6, 2) ||
    SUBSTR(CHAR(CURRENT DATE, ISO), 9, 2)
FROM SYSIBM.SYSDUMMY1

Current date is in yyyy-mm-dd format. 当前日期为yyyy-mm-dd格式。 You can convert it into yyyymmdd format using substring function: 您可以使用substring函数将其转换为yyyymmdd格式:

select substr(current date,1,4)||substr(current date,6,2)||substr(currentdate,9,2)
select to_char(current date, 'yyyymmdd') from sysibm.sysdummy1

结果:20160510

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

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