GETDATE() method for DB2
Asked Answered
C

3

19

I have been trying for a while now to get a similar method to GETDATE() in DB2 for i. So far I have found the following:

current date
current timestamp
current time

Would it be possible for me to:

 select specific, columns
 from table
 where datefield = current date - 1 day

Is this the most efficient way or is there some way I perhaps haven't found yet?

EDIT:

I currently have this:

WHERE datefield = - days(date('2013-10-28'))

although this isn't helpful as I will need to edit it every day the query runs.

Have now come to this:

WHERE datefield = VARCHAR_FORMAT(CURRENT TIMESTAMP, 'YYYYMMDD') - 1

Except this will not work on the first day of the month as 1 - 1 = 0 and there is no day 0 in a month...

Cuckooflower answered 30/10, 2013 at 9:59 Comment(5)
What do you exactly want to achieve ? What is your question ? Do you want a day previous to current day ?Speechmaker
Here's a little article that might help you out: ibm.com/developerworks/data/library/techarticle/0211yip/…Fathead
select dateadd(dd,-1,getdate()) This will give you the day previous to current day. If this answers you question ...Speechmaker
@Speechmaker - I'd say it's pretty clear that's what the OP is looking for - make that an answer.Inactivate
what's stopping you from using current_timestamp - 1?Bantustan
D
36

This will give you yesterday's date:

SELECT CURRENT DATE - 1 DAY FROM sysibm.sysdummy1
Dubai answered 30/10, 2013 at 14:12 Comment(0)
E
4

If you want to know a certain date range as an alternative you can try the TIMESTAMPDIFF scalar function, read syntax diagramSkip visual syntax diagram The parameter: 16 indicates it will evaluate per days

The next example determines a range of 70 days as of now.

WHERE (TIMESTAMPDIFF(16, CHAR(SYSDATE- CURRENT DATE)) )<70 AND (TIMESTAMPDIFF(16, CHAR(SYSDATE- CURRENT DATE)) ) > -1

you can take a look on this link for all the details about this method which is supported by DB2: http://www-01.ibm.com/support/knowledgecenter/SSEPGG_9.7.0/com.ibm.db2.luw.sql.ref.doc/doc/r0000861.html?cp=SSEPGG_9.7.0%2F2-10-3-2-155

Equestrienne answered 13/1, 2015 at 16:17 Comment(1)
The reference is for DB2 LUW. A DB2 for IBM i reference can be seen at www-01.ibm.com/support/knowledgecenter/ssw_ibm_i_72/db2/…Amsterdam
S
-6
select dateadd(dd,-1,getdate())
Speechmaker answered 30/10, 2013 at 12:0 Comment(1)
There is no DATEADD() in standard SQL. It's a Microsoft extension. MySQL also has Date_Add(). Similar for GETDATE().Futurity

© 2022 - 2024 — McMap. All rights reserved.