COGNOiSe.com - The IBM Cognos Community

IBM Cognos 10 Platform => Cognos 10 BI => Report Studio => Topic started by: cognos74 on 14 Sep 2016 03:18:18 PM

Title: How to separate number from the text and numeric togheher data.
Post by: cognos74 on 14 Sep 2016 03:18:18 PM
Hi, I have a question,

in my report one of the column has values like below,  I need to get only the numbers from it means 15,45 etc..
Individual;15
Individual;45
Group:45
Group;30
etc..
any idea how to get that. I think using position function it might possible but no idea how to use that function I never used.

Title: Re: How to separate number from the text and numeric togheher data.
Post by: sdf on 14 Sep 2016 09:41:40 PM
have you tried SUBSTR function?
i can see you different number of characters(before the numbers), so you might need to use CHAR_LENGTH as well.
Title: Re: How to separate number from the text and numeric togheher data.
Post by: Lynn on 15 Sep 2016 03:29:05 AM
The best place to do these types of string manipulations is in the database, not in the report. I realize that isn't always an option but it never stops me from saying it anyway.

So if you must do it in the report, can you tell us what database you use? Presumably this is a relational data source?

The DB2 "translate" function is illustrated below. The first argument is the thing to be translated so you'd put a reference to your query item in there, not the string example as I've done. The second argument is the thing to replace the unwanted characters with, which is a blank in the below example. The third argument contains all the unwanted characters. I've used all the upper case and lower case letters plus a semi-colon based on the example you provided. If you have other special characters like % or * or whatever you'd need to include those as well.


trim ( translate('Individual;15',' ', 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ;' ) )


The result of the translate function would be '           15' and wrapping it in a trim function gets rid of any leading or trailing blanks leaving you with '15'. You could wrap a cast function around that to leave you with a number 15 if that's where you're going.

I believe oracle has a translate function although the syntax might be different. I think SQL Server has a replace function.
Title: Re: How to separate number from the text and numeric togheher data.
Post by: bdbits on 15 Sep 2016 09:33:54 AM
Totally agree with Lynn this should be done in ETL, but...

I think this will do it.
substring('Individual;15',position(';','Individual;15'))

If those examples are actually a data item named [SomeItem], then this.
substring([SomeItem],position(';',[SomeItem]))