If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > DB2 > DB2 Newbie - Query Help

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 10-13-10, 13:46
maxwellhouse maxwellhouse is offline
Registered User
 
Join Date: Oct 2010
Posts: 8
DB2 Newbie - Query Help

I am a SQL Server guy, was hoping someone could help me accomplish the following using DB2.

The below query works fine in SQL Server. I am having difficulties doing something similar in DB2.
Specially, I am trying to pull all records out of a DB2 table
WHERE
TRAN_TS >= first day of previous month
AND
TRAN_TS < first day of current month

WORKING SQL CODE:

select * from atable
where
TRAN_TS >= DateAdd(month, DateDiff(month, 0, getdate())-1, 0)
and
TRAN_TS < DateAdd(month, DateDiff(month, 0, getdate()), 0)

Does anyone know how to do the same in DB2?

Thanks
Reply With Quote
  #2 (permalink)  
Old 10-13-10, 15:21
mdx34 mdx34 is offline
Registered User
 
Join Date: Oct 2004
Posts: 238
You need to calculate the dates in DB2. How about,

first_day_last_month="select date('0001-01-01') + year(current date) years - 1 year + month(current date) months - 2 months from sysibm.sysdummy1"

first_day_this_month="select date('0001-01-01') + year(current date) years - 1 year + month(current date) months - 1 month from sysibm.sysdummy1"

Last edited by mdx34; 10-13-10 at 15:36.
Reply With Quote
  #3 (permalink)  
Old 10-13-10, 15:36
tonkuma tonkuma is offline
Registered User
 
Join Date: Feb 2008
Location: Japan
Posts: 2,193
There are some ways(I like first examples):

first day of previous month ===> LAST_DAY(CURRENT DATE) + 1 DAY - 2 MONTHs
first day of previous month ===> CURRENT DATE - ( DAY(CURRENT DATE) - 1 ) DAYs - 1 MONTH

first day of current month ===> LAST_DAY(CURRENT DATE) + 1 DAY - 1 MONTH
first day of current month ===> CURRENT DATE - ( DAY(CURRENT DATE) - 1 ) DAYs
first day of current month ===> DATE( TRANSLATE( 'abcd-ef-01' , HEX(CURRENT DATE) , 'abcdefgh' ) )
Reply With Quote
  #4 (permalink)  
Old 10-13-10, 23:23
Marcus_A Marcus_A is online now
Registered User
 
Join Date: May 2003
Location: USA
Posts: 5,198
You forgot the ye olde substring function:

first day of current month ===> substr(char(current_date,iso),1,8)||'01'
__________________
M. A. Feldman
IBM Certified DBA on DB2 for Linux, UNIX, and Windows
IBM Certified DBA on DB2 for z/OS and OS/390
Reply With Quote
  #5 (permalink)  
Old 10-14-10, 09:37
goldfishhh goldfishhh is offline
Registered User
 
Join Date: Nov 2004
Posts: 54
Quote:
Originally Posted by maxwellhouse View Post
I am a SQL Server guy, was hoping someone could help me accomplish the following using DB2.

The below query works fine in SQL Server. I am having difficulties doing something similar in DB2.
Specially, I am trying to pull all records out of a DB2 table
WHERE
TRAN_TS >= first day of previous month
AND
TRAN_TS < first day of current month

WORKING SQL CODE:

select * from atable
where
TRAN_TS >= DateAdd(month, DateDiff(month, 0, getdate())-1, 0)
and
TRAN_TS < DateAdd(month, DateDiff(month, 0, getdate()), 0)

Does anyone know how to do the same in DB2?

Thanks

You could also do this (I adapted it for your field)

Code:
,CASE WHEN (YEAR(TRAN_TS 12+                        
            (MONTH(TRAN_TS ))) =                       
(                                                             
SELECT (YEAR(CURRENT DATE)*12+                           
(MONTH(CURRENT DATE) -1))                                
FROM SYSIBM.SYSDUMMY1                                         
)                                                             
       THEN 1 ELSE 0 END             -- IS_IN_PRIOR_MNTH_NBR,     
,CASE WHEN (YEAR(TRAN_TS 12+                        
            (MONTH(TRAN_TS ))) =                       
(                                                             
SELECT (YEAR(CURRENT DATE)*12+                           
(MONTH(CURRENT DATE) ))                                
FROM SYSIBM.SYSDUMMY1                                         
)                                                             
       THEN 1 ELSE 0 END             -- IS_IN_CURRENT_MNTH_NBR,
Reply With Quote
  #6 (permalink)  
Old 10-14-10, 10:48
maxwellhouse maxwellhouse is offline
Registered User
 
Join Date: Oct 2010
Posts: 8
Almost there....

Good stuff guys....!

Should "MONTHs" really read "MONTH"?


LAST_DAY(CURRENT DATE) + 1 DAY - 2 MONTHs
Reply With Quote
  #7 (permalink)  
Old 10-14-10, 12:46
tonkuma tonkuma is offline
Registered User
 
Join Date: Feb 2008
Location: Japan
Posts: 2,193
Quote:
Should "MONTHs" really read "MONTH"?
Both are OK.

IBM DB2 9.7 for Linux, UNIX, and Windows SQL Reference, Volume 1
Chapter 2. Language elements ---> Expressions ---> Datetime operations and durations ---> Durations
Quote:
.....

A labeled duration represents a specific unit of time as expressed by a number
(which can be the result of an expression) followed by one of the seven duration
keywords: YEARS, MONTHS, DAYS, HOURS, MINUTES, SECONDS, or
MICROSECONDS. (The singular form of these keywords is also acceptable: YEAR,
MONTH, DAY, HOUR, MINUTE, SECOND, and MICROSECOND.) .....
I like to use plural form for duration, except a number is a literal one(i.e. 1).
One reason is that the principal clearly distinguish between durations and functions.
Like this:
Quote:
CURRENT DATE - ( DAY(CURRENT DATE) - 1 ) DAYs
DAY is a function.
DAYs is a duration.

Last edited by tonkuma; 10-14-10 at 12:59. Reason: Add my opinion.
Reply With Quote
  #8 (permalink)  
Old 10-14-10, 12:55
Lenny77 Lenny77 is offline
Registered User
 
Join Date: Jul 2009
Location: NY
Posts: 886
Thumbs up between is better

You have to change:

Quote:
TRAN_TS >= first day of previous month
AND
TRAN_TS < first day of current month
to

Code:
TRAN_TS between 
last_day(current date - 2 month) + 1 day 
AND
last_day(current date - 1 month)
Lenny
Reply With Quote
  #9 (permalink)  
Old 10-14-10, 13:13
maxwellhouse maxwellhouse is offline
Registered User
 
Join Date: Oct 2010
Posts: 8
Thanks.

Lenny, yours is pretty easy to understand.
Much thanks!
Reply With Quote
  #10 (permalink)  
Old 10-14-10, 13:25
Lenny77 Lenny77 is offline
Registered User
 
Join Date: Jul 2009
Location: NY
Posts: 886
It has to work, too !
Reply With Quote
  #11 (permalink)  
Old 10-19-10, 17:43
tonkuma tonkuma is offline
Registered User
 
Join Date: Feb 2008
Location: Japan
Posts: 2,193
DB2 9.7 for LUW supports TRUNC function for datetime expression, like this .....

Code:
TRAN_TS between 
TRUNC(current date - 1 MONTH , 'MM')
AND
last_day(current date - 1 month)
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On