20

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?

acdcjunior
  • 124,334
  • 35
  • 321
  • 293
zod
  • 11,702
  • 23
  • 66
  • 104

5 Answers5

45
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().

bhamby
  • 14,744
  • 1
  • 44
  • 63
  • that works . thanks .. can you also tell how can i substarct days from the above date ........................................................ like CURDATE() - ( 3 day ) – zod Apr 25 '12 at 18:49
  • 1
    It looks like you already have it. :) `CURRENT TIMESTAMP - 3 DAY` – bhamby Apr 25 '12 at 19:05
  • 1
    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. – Abel C Apr 06 '18 at 16:07
4

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

Joshua Balan
  • 143
  • 1
  • 2
2
select to_char(current date, 'yyyymmdd') from sysibm.sysdummy1

result: 20160510

Paul Roub
  • 35,848
  • 27
  • 79
  • 88
  • The query not working. It return : SQL0440N No authorized routine named "TO_CHAR" of type "FUNCTION" having compatible arguments was found. SQLSTATE=42884 – Ian Dec 14 '17 at 03:37
  • 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._ – not2savvy Mar 03 '20 at 12:59
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
Gilbert Le Blanc
  • 48,182
  • 6
  • 65
  • 110
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)
Tisho
  • 7,870
  • 6
  • 42
  • 52
albin
  • 11
  • 1