Date conversation

Post questions here relative to DataStage Enterprise/PX Edition for such areas as Parallel job design, Parallel datasets, BuildOps, Wrappers, etc.

Moderators: chulett, rschirm, roy

srini.dw
Premium Member
Premium Member
Posts: 186
Joined: Fri Aug 18, 2006 1:59 am
Location: Chennai

Date conversation

Post by srini.dw »

Hi,

I am having a paramter as 2007-01-01, when i run the job i need the following logic to be implemented

1 . select TO_DATE ('12/01/'||TO_CHAR(TO_NUMBER(TO_CHAR(SYSDATE,'YYYY')) -1),'MM/DD/YYYY')
from dual;

i.,e it should give me 01-DEC-06 as a result. (12/01 is a hardcode values)

2. select TO_DATE(TO_CHAR(TO_NUMBER(TO_CHAR(SYSDATE,'MM'))-1)||'/01/'||TO_CHAR(SYSDATE,'YYYY'),'MM
/DD/YYYY') from dual;

i.,e it should give me 01-FEB-07 (previous month first date)

Thanks,
DSguru2B
Charter Member
Charter Member
Posts: 6854
Joined: Wed Feb 09, 2005 3:44 pm
Location: Houston, TX

Post by DSguru2B »

Have you tried doing it yourself? What have you tried so far? It can be done easily using out of the box functions.
Creativity is allowing yourself to make mistakes. Art is knowing which ones to keep.
kcbland
Participant
Posts: 5208
Joined: Wed Jan 15, 2003 8:56 am
Location: Lutz, FL
Contact:

Post by kcbland »

Where do you need this? In a stage variable? In a query? In a filename?
Kenneth Bland

Rank: Sempai
Belt: First degree black
Fight name: Captain Hook
Signature knockout: right upper cut followed by left hook
Signature submission: Crucifix combined with leg triangle
DSguru2B
Charter Member
Charter Member
Posts: 6854
Joined: Wed Feb 09, 2005 3:44 pm
Location: Houston, TX

Post by DSguru2B »

Another question, You query just includes current date. Whats the purpose of the parameter (2007-01-01) that you mentioned in your query?
Creativity is allowing yourself to make mistakes. Art is knowing which ones to keep.
trobinson
Participant
Posts: 208
Joined: Thu Apr 11, 2002 6:02 am
Location: Saint Louis
Contact:

Post by trobinson »

I thought this was a post on how to score with chicks.
DSguru2B
Charter Member
Charter Member
Posts: 6854
Joined: Wed Feb 09, 2005 3:44 pm
Location: Houston, TX

Post by DSguru2B »

....more like how do keep chicks interested. :wink:
Creativity is allowing yourself to make mistakes. Art is knowing which ones to keep.
DSguru2B
Charter Member
Charter Member
Posts: 6854
Joined: Wed Feb 09, 2005 3:44 pm
Location: Houston, TX

Post by DSguru2B »

I am still not able to understand whats the use of the parameter if your not using it for your transformations. Anywho, the following should work
For #1:

Code: Select all

StringToDate(DecimalToString(YearFromDate(CurrentDate())-1):"-12-01", "%dd-%MMM-%yy")
and for #2:

Code: Select all

If MonthFromDate(CurrentDate()) = 1 then StringToDate(DecimalToString(YearFromDate(CurrentDate())-1):"-12-01", "%dd-%MMM-%yy") else StringToDate(DecimalToString(YearFromDate(CurrentDate()):"-":DecimalToString(MonthFromDate(CurrentDate()) - 1):"-01", "%dd-%MMM-%yy")
For the second query you have to check if present month is January, you will take the year, subtract 1 and then concatenate it with "-12-01".

NOTE: Use the above query in the initial values of stage variables. This way it will only be executed once.
Creativity is allowing yourself to make mistakes. Art is knowing which ones to keep.
ray.wurlod
Participant
Posts: 54607
Joined: Wed Oct 23, 2002 10:52 pm
Location: Sydney, Australia
Contact:

Post by ray.wurlod »

I thought it was talking about those fruits that originate in the Middle East.
:lol:
IBM Software Services Group
Any contribution to this forum is my own opinion and does not necessarily reflect any position that IBM may hold.
kumar_s
Charter Member
Charter Member
Posts: 5245
Joined: Thu Jun 16, 2005 11:00 pm

Post by kumar_s »

Why not the same given query in a Database stage.

Code: Select all

 select TO_DATE ('12/01/'||TO_CHAR(TO_NUMBER(TO_CHAR(SYSDATE,'YYYY')) -1),'MM/DD/YYYY')
from dual;

select TO_DATE(TO_CHAR(TO_NUMBER(TO_CHAR(SYSDATE,'MM'))-1)||'/01/'||TO_CHAR(SYSDATE,'YYYY'),'MM
/DD/YYYY') from dual; 
Impossible doesn't mean 'it is not possible' actually means... 'NOBODY HAS DONE IT SO FAR'
kumar_s
Charter Member
Charter Member
Posts: 5245
Joined: Thu Jun 16, 2005 11:00 pm

Post by kumar_s »

trobinson wrote:I thought this was a post on how to score with chicks.
Our site is not, that helpful. :?
Impossible doesn't mean 'it is not possible' actually means... 'NOBODY HAS DONE IT SO FAR'
DSguru2B
Charter Member
Charter Member
Posts: 6854
Joined: Wed Feb 09, 2005 3:44 pm
Location: Houston, TX

Re: Date conversation

Post by DSguru2B »

srini.dw wrote:2. select TO_DATE(TO_CHAR(TO_NUMBER(TO_CHAR(SYSDATE,'MM'))-1)||'/01/'||TO_CHAR(SYSDATE,'YYYY'),'MM
/DD/YYYY') from dual;
This query will break every time its run in January.
Creativity is allowing yourself to make mistakes. Art is knowing which ones to keep.
kumar_s
Charter Member
Charter Member
Posts: 5245
Joined: Thu Jun 16, 2005 11:00 pm

Post by kumar_s »

As suggested by DSguru2B, check for month 01 or 12, and subtract the year by 1. You can either use Decode() or Case() in SQL or If then Else in Tranformer.
Impossible doesn't mean 'it is not possible' actually means... 'NOBODY HAS DONE IT SO FAR'
srini.dw
Premium Member
Premium Member
Posts: 186
Joined: Fri Aug 18, 2006 1:59 am
Location: Chennai

Post by srini.dw »

Hi,

Thanks for the below derviations.
Code:
StringToDate(DecimalToString(YearFromDate(CurrentDate())-1):"-12-01", "%dd-%MM-%yyyy")

Code:
If MonthFromDate(CurrentDate()) = 1 then StringToDate(DecimalToString(YearFromDate(CurrentDate())-1):"-12-01", "%dd-%MMM-%yy") else StringToDate(DecimalToString(YearFromDate(CurrentDate()):"-":DecimalToString(MonthFromDate(CurrentDate()) - 1):"-01", "%dd-%mm-%yyyy")

But when i execute the above codes in transformer its giving me ******


So wat iam doing is i have 4 stage variables i.,e MM, YY, MMminusOne, YYminusOne (all integer type)

When i execute the below one its works (datatype date)
StringToDate(svYYminusOne:"-":"01-":"01")

but when i try to execute (datatype date) StringToDate(svYYminusOne:"-":DecimalToString(svMM):"01") its still giving ******

MM is MonthFromDate(currentdate()
YY is YearFromDate(currentdate()

Can anyone plz let me know why..

Thanks
DSguru2B
Charter Member
Charter Member
Posts: 6854
Joined: Wed Feb 09, 2005 3:44 pm
Location: Houston, TX

Post by DSguru2B »

The format was messed up. Use the following:
For #1

Code: Select all

StringToDate(DecimalToString(YearFromDate(CurrentDate())-1):"-12-01", "%yyyy-%mm-%dd") 
For #2

Code: Select all

If MonthFromDate(CurrentDate()) = 1 then StringToDate(YearFromDate(CurrentDate())-1:"-12-01", "%yyyy-%mm-%dd") else if Len(MonthFromDate(CurrentDate())-1) = 1 then StringToDate(YearFromDate(CurrentDate()):"-0":MonthFromDate(CurrentDate()) - 1:"-01", "%yyyy-%mm-%dd") else StringToDate(YearFromDate(CurrentDate()):"-":MonthFromDate(CurrentDate()) - 1:"-01", "%yyyy-%mm-%dd")
The date format was giving issues and hence you were seeing all stars. I fixed the format. You will get date in YYYY-MM-DD format. If you want it in any other format, do the above in a stage variable and pass that variable to DateToString(<<stgvar>>, "your format")
Another problem was cause by MonthFromDate(). It was returning a single digit month where as two digit month (%mm) is required. And hence I added another check.
Creativity is allowing yourself to make mistakes. Art is knowing which ones to keep.
srini.dw
Premium Member
Premium Member
Posts: 186
Joined: Fri Aug 18, 2006 1:59 am
Location: Chennai

Post by srini.dw »

Hi,

Thanks for the code, 2nd one works fine, but 1st one still giving **** in the output, (datatype date)

Thanks,
Post Reply