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 keyword with IN keyword

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #31 (permalink)  
Old 07-10-11, 23:54
tonkuma tonkuma is online now
Registered User
 
Join Date: Feb 2008
Location: Japan
Posts: 2,193
It may be better to make a UDF to decompose comma separated string into rows.

Here is an example using such kind of UDF.
Code:
------------------------------ Commands Entered ------------------------------
WITH state(city) AS (
VALUES
  ('Buffalo Grove')
, ('Buffer zone')
, ('Deerfield')
, ('DEERFIELD')
, ('Deerhound')
, ('deep lake')
, ('deer lake')
, ('deer')
)
SELECT city
     , VARCHAR(element , 20) || ';' AS input_city_abb
 FROM  state
 INNER JOIN
       TABLE( elements_of_list_v910('Buff, deer') ) t
   ON  SYSFUN.LOCATE( LTRIM(UPPER(element)) , UPPER(city) ) = 1
;
------------------------------------------------------------------------------

CITY          INPUT_CITY_ABB       
------------- ---------------------
Buffalo Grove Buff;                
Buffer zone   Buff;                
Deerfield      deer;               
DEERFIELD      deer;               
Deerhound      deer;               
deer lake      deer;               
deer           deer;               

  7 record(s) selected.
I think that already there are some examples.
But, it is more troblesome for me to search the examples than to create a new one.
So, I made two new examples(an UDF with a parameter calls an UDF with same name and two parameters).

Sample UDFs: Tested on DB2 9.7 for Windows.
Although I didn't test them on DB2 9.1, it may work on DB2 9.1 for LUW too.
Code:
------------------------------ Commands Entered ------------------------------
CREATE FUNCTION
 elements_of_list_v910
 ( in_list      VARCHAR(4000)
 )
 RETURNS TABLE
 ( element  VARCHAR(128)
 , sequence SMALLINT
 )
   READS SQL DATA
   NO EXTERNAL ACTION
   DETERMINISTIC
/* Function Body:
1) extracts elements
    delimitted by in_delimiter
     from in_list.
2) may work on DB2 for LUW 9.1 or 9.5
*/
RETURN
SELECT SUBSTR(
          in_list
        , 1 + COALESCE(
                 MAX(pos) OVER(ORDER BY pos
                               ROWS BETWEEN 1 PRECEDING
                                        AND 1 PRECEDING)
               , 0
              )
        , pos - 1
            - COALESCE(
                 MAX(pos) OVER(ORDER BY pos
                               ROWS BETWEEN 1 PRECEDING
                                        AND 1 PRECEDING)
               , 0
              )
       )
     , ROW_NUMBER() OVER(ORDER BY pos)
 FROM  (VALUES LENGTH(in_list) + 1) p(len_list_plus)
 INNER JOIN
       LATERAL
       (SELECT COALESCE( NULLIF(n1 + n2 + n3 , 0) , len_list_plus )
         FROM  (VALUES   0,   1,   2,   3,   4,   5,   6,   7
                     ,   8,   9,  10,  11,  12,  13,  14,  15 ) n(n1)
         INNER JOIN
               (VALUES   0,  16,  32,  48,  64,  80,  96, 112
                     , 128, 144, 160, 176, 192, 208, 224, 240 ) n(n2)
           ON  n2      < len_list_plus
           AND n1 + n2 < len_list_plus
         INNER JOIN
               (VALUES   0, 256, 512, 768,1024,1280,1536,1792
                     ,2048,2304,2560,2816,3072,3328,3584,3840 ) n(n3)
           ON  n3           < len_list_plus
           AND n1 + n2 + n3 < len_list_plus
         WHERE n1 < len_list_plus
       ) n(pos)
   ON  in_delimiter  = SUBSTR(in_list , NULLIF(pos , len_list_plus) , 1)
   OR  len_list_plus = pos
;
------------------------------------------------------------------------------
DB20000I  The SQL command completed successfully.
Code:
------------------------------ Commands Entered ------------------------------
CREATE FUNCTION
 elements_of_list_v910
 ( in_list      VARCHAR(4000)
 )
 RETURNS TABLE
 ( element  VARCHAR(128)
 , sequence SMALLINT
 )
   READS SQL DATA
   NO EXTERNAL ACTION
   DETERMINISTIC
/* Function Body:
1) extracts elements
    delimitted by in_delimiter
     from in_list.
2) may work on DB2 for LUW 9.1 or 9.5
*/
RETURN
SELECT *
 FROM  TABLE( elements_of_list_v910(in_list , ',') ) t
;
------------------------------------------------------------------------------
DB20000I  The SQL command completed successfully.

Last edited by tonkuma; 07-10-11 at 23:59. Reason: Add correlation name t to "an example using such kind of UDF"
Reply With Quote
  #32 (permalink)  
Old 07-14-11, 12:21
ashu000 ashu000 is offline
Registered User
 
Join Date: Apr 2011
Posts: 28
Hi Tonkuma,

Sorry for the late response.

Thanks for your help once again.
Reply With Quote
Reply

Tags
in keyword, select

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