Skip to Main Content

SQL & PL/SQL

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Count of word match in two columns

NSK2KSNMay 9 2014 — edited May 12 2014

Below are my DDL and DML Statements

CREATE TABLE a1

(

   col1   VARCHAR2 (255 CHAR) NOT NULL,

   col2   VARCHAR2 (255 CHAR) NOT NULL

);

INSERT INTO a1 (col1, col2)

     VALUES ('I love my pet', 'I love Sachin');

INSERT INTO a1 (col1, col2)

     VALUES ('I hate you', 'hate you');

INSERT INTO a1 (col1, col2)

     VALUES ('Love me', 'Love you');

Expected Output

Col1Col2Word_Match
I love my petI love Sachin2
I hate youhate you0
Love meLove you1

  1. For first row as in col1 and col2 there are two matched words from beginning of string desired output is 2 ( i.e., I in col1 is matched with I in col2 and love in col1 is matched with love in col2, But my in col1 is not matched with sachin, so comparison should stop and 2 should be returned as output)
  2. For second row as in col1 and col2 there are no matched words from beginning of string, desired output is 0  (i.e., I in col1 will be compared with hate in col2 as these are not matched, comparison should stop and 0 should be returned)
  3. For third row as in col1 and col2 there is one matched from beginning of string desired output is 1  (i.e., Love in col1 will be compared with Love in col2, These are matched and comparison continues with second word. Me is compared with you which is not matched so comparison should stop and 1 should be returned        

Am looking, if we can acheive this in SQL rather than PLSQL. I have achieved the desired output in PLSQL.

Thanks for your suggestions in advance.

This post has been answered by SKP on May 12 2014
Jump to Answer
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jun 9 2014
Added on May 9 2014
8 comments
1,183 views