DB2 Date format
Asked Answered
U

5

23

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?

Ursel answered 25/4, 2012 at 17:42 Comment(0)
C
49
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().

Caprice answered 25/4, 2012 at 18:18 Comment(3)
that works . thanks .. can you also tell how can i substarct days from the above date ........................................................ like CURDATE() - ( 3 day )Ursel
It looks like you already have it. :) CURRENT TIMESTAMP - 3 DAYCaprice
Keep in mind once converted the result may include blanks to the right of values, depending on the db2 version. The column result will be 255 long.Brufsky
B
4

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

Birmingham answered 18/4, 2013 at 19:11 Comment(0)
N
4
select to_char(current date, 'yyyymmdd') from sysibm.sysdummy1

result: 20160510

Nit answered 10/5, 2016 at 17:15 Comment(2)
The query not working. It return : SQL0440N No authorized routine named "TO_CHAR" of type "FUNCTION" having compatible arguments was found. SQLSTATE=42884Sellingplater
Works here - shortest and best solution for me. Cited from DB2 docs: The TO_CHAR scalar function is a synonym for the VARCHAR_FORMAT scalar function.Anacoluthia
B
1

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
Blent answered 25/4, 2012 at 17:59 Comment(2)
thanks .. but is this the only way to do it? then what is the use of formats .... DATE_K YYYYMMDD ??Ursel
I couldn't get the ALTDATE function to work on my DB2. What I posted in my answer works.Blent
C
1

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)
Crossman answered 24/7, 2012 at 18:7 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.