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 > Fun with Dates

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 01-12-10, 11:43
maw230 maw230 is offline
Registered User
 
Join Date: Dec 2009
Posts: 9
Fun with Dates

As an extremely new user to DB2 syntax, I am still having a difficult time with the DATE functions. I need a month-to-date function that I can write in a pass through query in Access to a DB2 server. I have received great help with a year -to-date function from this forum, and one would think changing to MTD wouldn't be such a leap. Here is the SQL for the year-to-date query that forum users helped me with:

Code:
SELECT LINE, CASH_UNITS,
CHARGE_UNITS, CASH_SALES, 
CHARGE_SALES, CASH_SALES_AT_COST,  
CHARGE_SALES_AT_COST
FROM DWQRYDATA.SLSDSI
Where 
DATE_ID BETWEEN 
current date - (dayofyear(current date) - 1)  days - 1 year  
    AND CURRENT_DATE - 1 YEAR
group by LINE, CASH_UNITS,  CHARGE_UNITS,  
CASH_SALES, CHARGE_SALES, 
CASH_SALES_AT_COST,  
CHARGE_SALES_AT_COST
order by LINE
Now, what I need is a Month-to-date function that will bring back this years and last year's month-to-date values. I assume this would be done with a: BETWEEN [this year functions] OR BETWEEN [last year functions]. Please help as I think using the DB2 server to do my calculations would help the performance of my query greatly, as compared to using ODBC links through Access. Thanks.
Reply With Quote
  #2 (permalink)  
Old 01-12-10, 11:56
Lenny77 Lenny77 is offline
Registered User
 
Join Date: Jul 2009
Location: NY
Posts: 886
instead of current date - (dayofyear(current date) - 1) days - 1 year ,
use current date - (day(current date) - 1) days

Also, you can use last_day(current date - 1 month) + 1 day.

Lenny

Last edited by Lenny77; 01-12-10 at 12:01.
Reply With Quote
  #3 (permalink)  
Old 01-12-10, 12:28
maw230 maw230 is offline
Registered User
 
Join Date: Dec 2009
Posts: 9
ok, that gets me current year MTD values?
What about last year's? surely a "-1" goes somewhere?
Reply With Quote
  #4 (permalink)  
Old 01-12-10, 13:59
Lenny77 Lenny77 is offline
Registered User
 
Join Date: Jul 2009
Location: NY
Posts: 886
Quote:
Originally Posted by maw230 View Post
ok, that gets me current year MTD values?
What about last year's? surely a "-1" goes somewhere?
That's give you start of the current month. Nothing else.

For get start of the current year you have to use: current date - (dayofyear(current date) - 1) days

Lenny
Reply With Quote
  #5 (permalink)  
Old 01-12-10, 17:04
maw230 maw230 is offline
Registered User
 
Join Date: Dec 2009
Posts: 9
thanks lenny, i will see what i can do with that
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