COGNOiSe.com - The IBM Cognos Community

IBM Cognos 8 Platform => COGNOS 8 => Report Studio => Topic started by: gatorfe on 22 Feb 2012 01:44:56 PM

Title: Date Calculation Sorting
Post by: gatorfe on 22 Feb 2012 01:44:56 PM
Hello, I have a calculation were I am extracting the month out of a date field.  When I sort this calculated field it does not sort 1,2,3,4,5,6,7,8,9,10,11,12. It does 10,11,12,1,2,3,4,5,6,7,8,9.  The calculation reads Cast(extract (month,[Invoice Date]),varchar(10)).  Is there anything I can add to calculation to make it sort as 1,2,3,4 etc order?  Thanks in advance for any suggestions.
Title: Re: Date Calculation Sorting
Post by: Lynn on 22 Feb 2012 01:52:26 PM
Does it have to be varchar? If you remove the cast and just extract the month it will be an integer and should sort correctly.

Otherwise you could left pad with zero so you end up with 01, 02, etc. The syntax for that could vary depending on what database functions you have available.

For me (in the DB2 torture chamber) I do this by taking the two right-most digits of a concatenation:

right ( '00' || Cast( extract ( month, [Invoice Date] ), varchar(10) ) , 2 )
Title: Re: Date Calculation Sorting
Post by: gatorfe on 22 Feb 2012 02:03:55 PM
Thank you Lynn!