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 > SQL to get substrings from within a table

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 02-26-11, 01:27
rocker86 rocker86 is offline
Registered User
 
Join Date: Jul 2009
Posts: 37
SQL to get substrings from within a table

Hi,
Here's what my table looks like:

Table:

Id-------num---------Path
100------1------------/1/
101------2------------/1/2/
102------2------------/2/
103------3------------/1/2/3/
104------4------------/1/2/3/4/
105------4------------/3/4/
106------4------------/1/4/

now I want a result in which the path is the ending subpath of an existing path. My resultset is something like this:

Id-------num----------Path
102------2------------/2/
105------4------------/3/4/

So the path /2/ was returned because it was the ending path of Id 101
And path /3/4/ was returned for the same reason, similar ending path for Id 104.

I tried the following query,

select A.* from table A where A.Id in(
select B.Id from table B
where
A.PATH like concat('%',B.PATH))

But it throws an SQLSTATE 56098 exception.
could any one please suggest an alternate query to do this?
Would really appreciate some help.
Thanks
Reply With Quote
  #2 (permalink)  
Old 02-26-11, 03:53
tonkuma tonkuma is online now
Registered User
 
Join Date: Feb 2008
Location: Japan
Posts: 2,195
Quote:
But it throws an SQLSTATE 56098 exception.
I don't know the reason of SQLSTATE 56098.

But, you can't specify a column name(or concat with a column name) to the right of a LIKE predicate.
If specified, you would get SQL0132N.
Quote:
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.
.....
Here is an alternative:
Code:
------------------------------ Commands Entered ------------------------------
WITH
/**************************************************
**********    Start of sample data.      **********
**************************************************/
table(Id , num , Path) AS (
VALUES
  (100 , 1 , '/1/')
, (101 , 2 , '/1/2/')
, (102 , 2 , '/2/')
, (103 , 3 , '/1/2/3/')
, (104 , 4 , '/1/2/3/4/')
, (105 , 4 , '/3/4/')
, (106 , 4 , '/1/4/')
)
/**************************************************
**********      End of sample data.      **********
**************************************************/

SELECT a.*
 FROM  table a
 WHERE EXISTS
       (SELECT 0
         FROM  table b
         WHERE b.num = a.num
           AND LOCATE(a.path , b.path , 2)
               = LENGTH(b.path) - LENGTH(a.path) + 1
       )
;
------------------------------------------------------------------------------

ID          NUM         PATH     
----------- ----------- ---------
        102           2 /2/      
        105           4 /3/4/    

  2 record(s) selected.

Last edited by tonkuma; 02-26-11 at 04:00. Reason: Change second predicate in WHERE clause of EXISTS subquery.
Reply With Quote
  #3 (permalink)  
Old 02-28-11, 04:13
rocker86 rocker86 is offline
Registered User
 
Join Date: Jul 2009
Posts: 37
Thanks a ton tonkuma! That worked like a charm
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