22

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
  • 132,397
  • 37
  • 331
  • 304
zod
  • 12,092
  • 24
  • 70
  • 106

5 Answers5

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

bhamby
  • 15,112
  • 1
  • 45
  • 66
  • 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
3
select to_char(current date, 'yyyymmdd') from sysibm.sysdummy1

result: 20160510

Paul Roub
  • 36,322
  • 27
  • 84
  • 93
  • 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
  • 50,182
  • 6
  • 67
  • 111
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
  • 8,320
  • 6
  • 44
  • 52
albin
  • 11
  • 1