Smart questions
Smart answers
Smart people
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Member Login

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips now!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

Join Tek-Tips
*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

LINK TO THIS FORUM!

Add Stickiness To Your Site By Linking To This Professionally Managed Technical Forum.
Just copy and paste the
code below into your site.

Partner With Us!

"Best Of Breed" Forums Add Stickiness To Your Site
Partner Button
(Download This Button Today!)

Feedback

"...I have never been to any technical site that shows concern just to anybody with problems...I look forward to also share in the future..."

Geography

Where in the world do Tek-Tips members come from?

I need to subtract one day from current date in DB2

jorame (Programmer)
22 Mar 12 17:09
Hello,

I have this query in DB2

SELECT * FROM DTINBS WHERE DTDCR = DAYS(CURRENT_DATE) - 1

The format for the where clause should be in YYYYMMDD for it work properly. If I change this query this

SELECT * FROM DTINBS WHERE DTDCR = 20120322 - 1

Everything works no problem, but I should be able to get today's date and subtract one day.

Any help will be appreciate it.

Thank you.
mikrom (Programmer)
23 Mar 12 4:34
CURRENT_DATE delivers result in DATE format, but there is possible to store date in numeric format with 8 digits /e.g. DECIMAL(8,0)/ as YYYYMMDD too (often used in COBOL programs).

If this is your case, then you have to do a conversion from DATE to number - For example something like this:

CODE

select                     
  CURRENT_DATE as TODAY_DATE,

  (CURRENT_DATE - 1 DAY) as YESTERDAY_DATE,

  YEAR(CURRENT_DATE)*10000 +
  MONTH(CURRENT_DATE)*100 +
  DAY(CURRENT_DATE) as TODAY_YYYYMMDD,

  YEAR(CURRENT_DATE - 1 DAY)*10000 +
  MONTH(CURRENT_DATE - 1 DAY)*100 +
  DAY(CURRENT_DATE - 1 DAY) as YESTERDAY_YYYYMMDD          
from sysibm.sysdummy1
;
Output:

CODE

TODAY_DATE  YESTERDAY_DATE  TODAY_YYYYMMDD   YESTERDAY_YYYYMMDD
2012-03-23    2012-03-22        20120323         20120322  
Now, when I have a table, where the record creation date DATCREATED is stored in numeric format YYYYMMDD, then I can do this selection, to get the records from yesterday:

CODE

select *
from MYLIB.MYTABLE
where
  DATCREATED =
  -- yesterday
  YEAR(CURRENT_DATE - 1 DAY)*10000 +
  MONTH(CURRENT_DATE - 1 DAY)*100 +
  DAY(CURRENT_DATE - 1 DAY)
;
MarcLodge (Programmer)
23 Mar 12 11:43
Jorame,
I think that your problem is that the column DTDCR is not a date but in fact an 8 digit number. You will need to convert that number to a date format before you can compare it to any date value. The best way to achieve this I think is as follows:

CODE

SELECT * FROM DTINBS
WHERE DATE(SUBSTR(CHAR(DTDCR),1,4)
           ||'-'||
           SUBSTR(CHAR(DTDCR),5,2)
           ||'-'||
           SUBSTR(CHAR(DTDCR),7,2)) = CURRENT_DATE - 1 DAY

This should give you what you are after.

Marc

 

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Back To Forum

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close