DB2 Date format

2019-03-24 22:21发布

I just want to format current date into yyyymmdd in DB2.

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

SELECT CURDATE() FROM SYSIBM.SYSDUMMY1;

I dont see any straightforward way to use the above listed formats.

Any suggestion?

5条回答
做个烂人
2楼-- · 2019-03-24 22:31

Current date is in yyyy-mm-dd format. You can convert it into yyyymmdd format using substring function:

select substr(current date,1,4)||substr(current date,6,2)||substr(currentdate,9,2)
查看更多
兄弟一词,经得起流年.
3楼-- · 2019-03-24 22:47

This isn't straightforward, but

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

returns the current date in yyyy-mm-dd format. You would have to substring and concatenate the result to get 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
查看更多
ら.Afraid
4楼-- · 2019-03-24 22:47
select to_char(current date, 'yyyymmdd') from sysibm.sysdummy1

result: 20160510

查看更多
唯我独甜
5楼-- · 2019-03-24 22:48
SELECT VARCHAR_FORMAT(CURRENT TIMESTAMP, 'YYYYMMDD')
FROM SYSIBM.SYSDUMMY1

Should work on both Mainframe and Linux/Unix/Windows DB2. Info Center entry for VARCHAR_FORMAT().

查看更多
爷的心禁止访问
6楼-- · 2019-03-24 22:52

One more solution REPLACE (CHAR(current date, ISO),'-','')

查看更多
登录 后发表回答