COGNOiSe.com - The IBM Cognos Community

IBM Cognos 10 Platform => Cognos 10 BI => Report Studio => Topic started by: Cognos Talk on 07 Aug 2013 07:53:21 AM

Title: Using Promptmany with like or contains commands
Post by: Cognos Talk on 07 Aug 2013 07:53:21 AM
I created select and search prompt and should be able to select multiple values.
When creating a filter I run in to the problem:
Filter using command "in" works fine: [TestItem] in (#promptmany('pTestItem')#)
Then I need to create another filter, using "like" command:
[Test1] like '%'|| (#promptmany('pTest1')#)||'%'
It only works if I select one item from select and search prompt, when selecting multiple items it gives me an error:
V5 syntax error found in expression "[Statute] like '%'|| ('CC-271(1)';'CC-181')||'%'", invalid token ";" found after "[Statute] like '%'|| ('CC-271(1)'".

For some reason it separates the values using ";" instead of "," (coma)
The same error comes when I use contains. [Test2] contains (#promptmany('pTest2')#), How I can fix the filter to report on multiple selected values?
I use Cognos Version 10.2

Thank you very much for your help,
MK

Title: Re: Using Promptmany with like or contains commands
Post by: calson33 on 07 Aug 2013 11:55:58 AM
Like and contains only work on single values, IN can work with a set of values.

To do what you want, you would need to modify your return values so that they build a "LIKE" for each selected item within your query.

So, instead of
where
....
......
and [Test1] like '%'|| (#promptmany('pTest1')#)||'%'

You would have
where
....
......
#promptmany('pTest1')#


where the promptmany would resolve to
and [Test1] like '%MYVALUE1%' and [Test1] like '%MYVALUE2%'
etc.
Where
and [Test1] like '%MYVALUE1%'
is the return value for a single selection.

It is a bit more complicated than what I have written, but that is the general direction. Maybe some of the real experts around here could help you out with the syntax if you run into problems.
Title: Re: Using Promptmany with like or contains commands
Post by: CognosPaul on 07 Aug 2013 11:43:08 PM
You can generate the filter using macro expressions on a promptmany:

[Test1] like
#
join('%'' and [Test1] like ''%' +split(';',promptmany('pTest1','token','','''%','','%'''))
#

I'm not at a Cognos install, but I think this should work.


If the user selects a;b, it will
1. wrap it in '%a;b%'
2. split it into an array on the ; ['%a][b%']
3. Join it as '%a%' and [Test1] like '%b%'
Title: Re: Using Promptmany with like or contains commands
Post by: tschuman13 on 25 Mar 2014 02:37:30 PM
Was anyone able to get this to work?  Need a solution similar to these but am getting errors trying to run it.
Title: Re: Using Promptmany with like or contains commands
Post by: CognosPaul on 25 Mar 2014 03:03:39 PM
Please see rule 1 from the forum etiquette (http://www.cognoise.com/index.php/topic,24030.0.html) post.
Title: Re: Using Promptmany with like or contains commands
Post by: cognos2014 on 27 Apr 2016 01:00:21 PM
I have a similar problem where multi-select values are selected in the prompt and need to be used with LIKE in my query. I tried the solution suggested above and get a parsing error. Any help appreciated as I don't work much with macros.

thanks!

Example:
Prompt1 Options (multi-select)
A
B
C
D
E

Dataitem1 is in this format - A,B,C

[Dataitem1] like
#
join('%'' and [Dataitem1] like ''%' +split(';',promptmany('prompt1','token','','''%','','%'''))
#
Title: Re: Using Promptmany with like or contains commands
Post by: kmahesh on 26 Apr 2017 02:15:14 PM
I got the same situation but used contains parameter instead of like. Below is the syntax I used and it worked..

[COLUMN_NAME] contains # join (''' or [COLUMN_NAME] contains ''', split(',', promptmany('PARAMETER_NAME')))#

In the above filter condition replace, Column Name with Report Field Name and Parameter Name with your parameter. Also you can replace contains with any other parameter.
Title: Re: Using Promptmany with like or contains commands
Post by: mavi1122 on 01 Oct 2017 06:41:30 AM
I have a report that has a multi line text box prompt where users can paste a list of names. The requirement is that regardless if the field is uppercase, lowercase or Initcap fields, it should be able to find the names from the source.

1) Used the following filter: #join('or',substitute('^','(upper([Data Item])) Contains ',split(';',promptmany('Parameter','string'))))#.  This appears to be working if the users manipulate their list of names to be all upper. Any suggestions on how it can be done without list of names manipulation?

2) I've tried upper('Parameter') but unsuccessful.

3) Tried replacing Contains with Like and included %wildcard, but the report just ends up bringing back everything(filter ignored).

4) Tried "starts with" function but didn't work either. Only retrieved how they were entered on the prompt box.

Should i be revamping my filter in it's entirety? Suggestions?
Title: Re: Using Promptmany with like or contains commands
Post by: CognosPaul on 03 Oct 2017 09:57:01 AM
Try:

#join('or',substitute('^','(upper([Data Item])) Contains ',split(';',
toupper(promptmany('Parameter','string'))
)))#
Title: Re: Using Promptmany with like or contains commands
Post by: mavi1122 on 08 Oct 2017 09:47:14 AM
CognosPaul that worked! 😊👍🏽 You're a genius. Truly appreciate your help sir.
Title: Re: Using Promptmany with like or contains commands
Post by: shradha on 04 Jan 2019 10:50:24 PM
Need help with this. Using it exactly as  (#join('or',substitute('^','(upper([Data Item])) Contains ',split(';',
toupper(promptmany('Parameter','string')))))#). But i get an error stating toupper/upper is not a macro function.

Any help to achieve this within macro. TIA.
Title: Re: Using Promptmany with like or contains commands
Post by: dougp on 07 Jan 2019 12:32:40 PM
In the case of this macro, upper is part of the SQL statement and toupper is a macro function.  If you are using it exactly, you shouldn't get that error message.  Can you paste the actual error message Cognos is giving you?
Title: Re: Using Promptmany with like or contains commands
Post by: CognosPaul on 07 Jan 2019 01:10:29 PM
toupper is a DQM only macro function. If you're not running DQM you would need to wrap each item in a database upper function.



#
join(
  ') or '
, substitute(
    '^'
    , 'upper([Data Item]) Contains upper('
    , split(';'
      , promptmany('Parameter','string',sq('abc'))
    )
  )
)
+')'
#
Title: Re: Using Promptmany with like or contains commands
Post by: dougp on 08 Jan 2019 11:55:39 AM
Brilliant!  I wasn't aware the list of available macro functions differed between DQM and CQM.  And Cognos is smart enough to omit toupper from the list of available macro functions if the package uses CQM.  I'll pay closer attention to what's available in the Expression Definition UI in the future.
Title: Re: Using Promptmany with like or contains commands
Post by: shradha on 10 Jan 2019 01:22:18 AM
thanks a lot. It worked. :)
One more, what changes should i do if i need to apply IN, in place of CONTAINS.
Sorry, new to macros, so can't explore much. :)
Title: Re: Using Promptmany with like or contains commands
Post by: CognosPaul on 10 Jan 2019 11:28:37 AM
can you post an example of what you're trying to achieve?
Title: Re: Using Promptmany with like or contains commands
Post by: shradha on 10 Jan 2019 01:24:10 PM
I basically have few multi prompt in my prompt page. The values has to be case insensitive. So i want to achieve something like upper([DataItem]) in upper(#promptmany('param')#). I can't use like/contain as i want to filter exact values as the input.
Thanks. :)
Title: Re: Using Promptmany with like or contains commands
Post by: CognosPaul on 10 Jan 2019 01:37:01 PM
This should do it:

#
'upper([Data Item]) in (upper('
+ join(
  '),upper( '
  , split(';'
    , promptmany('Parameter','string',sq('abc'))
  )
)
+'))'
#



End result will be something like:
upper([Data Item]) in (upper('a'),upper('b'),upper('c'))
Title: Re: Using Promptmany with like or contains commands
Post by: shradha on 10 Jan 2019 03:22:03 PM
it did work.  :) You're a genius :)
Title: Re: Using Promptmany with like or contains commands
Post by: Magdalina08 on 14 Jan 2020 01:22:47 PM
Quote from: CognosPaul on 07 Jan 2019 01:10:29 PM
toupper is a DQM only macro function. If you're not running DQM you would need to wrap each item in a database upper function.



#
join(
  ') or '
, substitute(
    '^'
    , 'upper([Data Item]) Contains upper('
    , split(';'
      , promptmany('Parameter','string',sq('abc'))
    )
  )
)
+')'
#


My issue was solved with this!  Thank you!  I've been working on this all day.   :D
Title: Re: Using Promptmany with like or contains commands
Post by: Magdalina08 on 17 Jan 2020 11:30:17 AM
Is there a way to use the promptmany macro similar to a case when statement?

Requirement:
We have offices A - Z
I need a multi-select prompt where users can choose A, B, F, R, T, and/or All (all will show all other values)

I have this working with a AND OR prompt, but it's not multi-select. 
Title: Re: Using Promptmany with like or contains commands
Post by: naughtyca on 30 Jan 2020 09:12:31 AM
Quote from: CognosPaul on 03 Oct 2017 09:57:01 AM
Try:

#join('or',substitute('^','(upper([Data Item])) Contains ',split(';',
toupper(promptmany('Parameter','string'))
)))#


sorry for being ignorant very new to macros, so im trying to create a prompt where a user can copy and paste multiple list of IDs in native SQL

Is this how i use this macro?

where a.ID contains
#join('or',substitute('^','(upper([a.ID])) Contains ',split(';',
toupper(promptmany('Parameter','string'))
)))#

its not working for me
thank you