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 > common part of strings

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 02-02-06, 06:22
nelapsi nelapsi is offline
Registered User
 
Join Date: Dec 2004
Posts: 43
common part of strings

hi all!
i have a table
Code:
create table t1
(
 name varchar(1000)
);
how can I find common part of strings in this table?
example:
Code:
select * from t1;
'asddddddddd'
'asdf'
'asdfgh'
'asdcvf'
common part will be 'asd'
Reply With Quote
  #2 (permalink)  
Old 02-02-06, 10:07
Wim Wim is offline
Registered User
 
Join Date: Nov 2004
Posts: 1,279
Code:
select * 
from t1
where name like 'asd%';
__________________
With kind regards . . . . . SQL Server 2000/2005/2008/2008 R2 Earned beers: 16
Wim
Beware of bugs in the above code; I have only proved it correct, not tried it. -- Donald Knuth
Grabel's Law: 2 is not equal to 3 -- not even for very large values of 2.
Pat Phelan's Law: 2 very definitely CAN equal 3 -- in at least two programming languages
Reply With Quote
  #3 (permalink)  
Old 02-03-06, 02:34
nelapsi nelapsi is offline
Registered User
 
Join Date: Dec 2004
Posts: 43
Quote:
Originally Posted by Wim
Code:
select * 
from t1
where name like 'asd%';
I don't know that strings in table and I want to find common part
Reply With Quote
  #4 (permalink)  
Old 02-03-06, 08:57
urquel urquel is offline
Registered User
 
Join Date: Aug 2004
Posts: 330
According to your logic, there would be several "common parts".

a, as, asd, s, sd, d

Maybe a more specific definition of common parts would make a solution more determinate.
Reply With Quote
  #5 (permalink)  
Old 02-05-06, 09:59
Peter.Vanroose Peter.Vanroose is offline
Registered User
 
Join Date: Sep 2004
Location: Belgium
Posts: 1,079
Anyway, there is no standard column function which does what you want.
You may implement such a function, though. It would probably look something like the following pseudo-code:
Code:
value = (SELECT MAX(name) FROM t1);
nonmatches = 0;
length = 0;
while (nonmatches = 0)
   length = length+1;
   prefix = SUBSTR(value,1,length);
   nonmatches = (SELECT COUNT(*) FROM t1 WHERE SUBSTR(name,1,length) <> prefix)
endwhile;
return SUBSTR(value,1,length-1);
__________________
--_Peter Vanroose,
__IBM Certified Database Administrator, DB2 9 for z/OS
__IBM Certified Application Developer
__ABIS Training and Consulting
__http://www.abis.be/
Reply With Quote
  #6 (permalink)  
Old 02-06-06, 08:59
gardenman gardenman is offline
Registered User
 
Join Date: Apr 2004
Posts: 54
Very interesting task.
I think you have to write yours own C/C++ function and keep your result for each time.
I wrote similar function called KEEP.
Here is the link:

http://www.sql****/forum/actualthread...244580&hl=keep
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