If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > DB2 > LIKE in a UDF

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 11-12-09, 18:46
gamo gamo is offline
Registered User
 
Join Date: Jul 2009
Location: Sydney
Posts: 13
LIKE in a UDF

Hi!

I am a bit stuck (DB2 9.5)

here is my UDF

Code:
CREATE FUNCTION myfunc( a varchar(1), b varchar(1) ) returns varchar( 1 )
DETERMINISTIC NO EXTERNAL ACTION 
BEGIN ATOMIC
DECLARE tmpstr varchar(2);
DECLARE result varchar(1);
SET tmpstr = a||b;
SET result  = select col from table where refcol like '%'||tmpstr||'%';
RETURN result;
END
and here is the error which I am getting:
SQL0132N A LIKE predicate or POSSTR scalar function is not valid because the first operand is not a string expression or the second operand is not a string

any ideas how I could properly cast the inputs so that I can use LIKE in this way??

Last edited by gamo; 11-12-09 at 18:52.
Reply With Quote
  #2 (permalink)  
Old 11-12-09, 23:18
stolze stolze is offline
Registered User
 
Join Date: Jan 2007
Location: Jena, Germany
Posts: 2,662
The patter for a LIKE predicate must be constant. In your case, it depends on the input parameters and is not constant.
__________________
Knut Stolze
IBM DB2 Analytics Accelerator
IBM Germany Research & Development
Reply With Quote
  #3 (permalink)  
Old 11-12-09, 23:21
gamo gamo is offline
Registered User
 
Join Date: Jul 2009
Location: Sydney
Posts: 13
Yes, I understand that - what I was wondering is if there is some way that I can work around this fact.
Reply With Quote
  #4 (permalink)  
Old 11-13-09, 00:14
stolze stolze is offline
Registered User
 
Join Date: Jan 2007
Location: Jena, Germany
Posts: 2,662
If you are just looking for substrings, you can use LOCATE(). If you want to allow more general patterns, regular expression matching may be something for you: Bringing the Power of Regular Expression Matching to SQL
__________________
Knut Stolze
IBM DB2 Analytics Accelerator
IBM Germany Research & Development
Reply With Quote
  #5 (permalink)  
Old 11-13-09, 00:14
rahul_s80 rahul_s80 is offline
Registered User
 
Join Date: Jul 2006
Location: Pune , India
Posts: 433
__________________
Rahul Singh
Certified DB2 9 DBA / Application Developer
Reply With Quote
  #6 (permalink)  
Old 11-13-09, 00:17
gamo gamo is offline
Registered User
 
Join Date: Jul 2009
Location: Sydney
Posts: 13
ah - thankyou - I didn't see that one!

helps when I search for the right thing
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On