COGNOiSe.com - The IBM Cognos Community

IBM Cognos 8 Platform => COGNOS 8 => Report Studio => Topic started by: Nandini.t on 13 Jan 2012 01:50:37 AM

Title: Months between two dates
Post by: Nandini.t on 13 Jan 2012 01:50:37 AM
Hi all,

I have a start date and End date.my date format is as 'YYYY/MM'. How to calculate months between start date and End date.

example:
I have start date = 1994/01 and End date = 2001/03

Thnks in advance fo any assistance.
Nandini
Title: Re: Months between two dates
Post by: blom0344 on 13 Jan 2012 03:23:24 AM
You mention 'date' format. Isn't this really stored as a string?
Title: Re: Months between two dates
Post by: Nandini.t on 13 Jan 2012 03:27:14 AM
No.
[START DATE] and [END DATE] both are in YYYY/MM format from the model itself.
Title: Re: Months between two dates
Post by: blom0344 on 13 Jan 2012 03:30:10 AM
and the standard _months_between function does not work?   8)
Title: Re: Months between two dates
Post by: charon on 13 Jan 2012 03:53:24 AM
 :o
Title: Re: Months between two dates
Post by: Nandini.t on 13 Jan 2012 03:57:13 AM
no its not working... its throwing error.

RQP-DEF-0177
An error occurred while performing operation 'sqlOpenResult' status='-28'.
Title: Re: Months between two dates
Post by: CognosPaul on 13 Jan 2012 04:30:27 AM
If its stored as YYYY/MM then it's not a date. You'll need to convert it back to a proper date before you can use the months between function. Which database are you using?
Title: Re: Months between two dates
Post by: Nandini.t on 13 Jan 2012 04:42:52 AM
oracle.
Title: Re: Months between two dates
Post by: blom0344 on 13 Jan 2012 06:13:33 AM
cast(substring([item],1,4),integer)*100+cast(substring([item],6,2),integer)

yields 199401 as numerical value which allows a regular subtraction between calculated items..
Title: Re: Months between two dates
Post by: absriram on 14 Jan 2012 05:47:55 PM
Quote from: blom0344 on 13 Jan 2012 06:13:33 AM
cast(substring([item],1,4),integer)*100+cast(substring([item],6,2),integer)

yields 199401 as numerical value which allows a regular subtraction between calculated items..

This won't work if the two dates are in different years.  For example 199401 vs 199501 will give 100 as the months between. The correct approach would be to use _make_timestamp.

Expression for Start Date: _make_timestamp (cast(substring([Start Date],1,4),integer), cast(substring([Start Date],6,2),integer), 1)
Expression for End Date: _make_timestamp (cast(substring([End Date],1,4),integer), cast(substring([End Date],6,2),integer), 1)

Expression for Months between: _months_between([End Date],[Start Date])

Sriram.
http://www.cognosonsteroids.com (http://www.cognosonsteroids.com)
Title: Re: Months between two dates
Post by: CognosPaul on 15 Jan 2012 02:02:21 AM
As it's Oracle, then the function to_date([Item]+'/01','YYYY/MM/DD') should work to convert it to a date.

Title: Re: Months between two dates
Post by: blom0344 on 15 Jan 2012 01:53:12 PM
silly me..

Should be:

cast(substring([item],1,4),integer)*12+cast(substring([item],6,2),integer)

I think 'correct' approach is a trifle too much. Multiple solutions may exist
Title: Re: Months between two dates
Post by: CognosPaul on 16 Jan 2012 07:53:57 AM
Ask 2 SQL experts how to do something and get 5 different answers. You could probably do some stress testing to see which solution is more efficient, but since they both use string operations performance won't be great. It would be better to find out who encoded the values as yyyy/mm and give them a smack.

Incidentally, I like Ties solution. It makes reduces date operations to simple arithmetic.