Click here to Skip to main content
15,437,171 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I am new to Pl/SQl. I have table Test with 3 columns.

Column1 Column2 Column3
-------------------------------------------------

100 CA,OH AR,CA

200 FL,MI MI,NY


So here in the table, when I compare column2 to column3, I have added AR and deleted OH for ID 100. For 200 I have deleted FL and added NY.

How can I find which string I have added in column 3 and which string I have deleted from Column2?

I need answer like ...

From 100, OH deleted and AR added.
From 200, FL deleted and NY added.

Can anyone please help me find out this?

Thank you very much in advance.

What I have tried:

I have tried strcomp function and it gave me whether the string has changed or not. But I need the exact string what has been changed.
Posted
Updated 4-Sep-17 18:16pm

1 solution

Following query will solve your problem

SQL
With Test_CTE(Column1,FirstColumn2,SecondColumn2,Column2,FirstColumn3,SecondColumn3,Column3)
AS(
Select column1,SUBSTRING(column2,1, charindex(',',Column2,1)-1) as FirstColumn2,
SUBSTRING(column2,charindex(',',Column2,1)+1,10) SecondColumn2,Column2,
SUBSTRING(column3,1, charindex(',',Column3,1)-1) as FirstColumn3,
SUBSTRING(column3,charindex(',',Column3,1)+1,10) SecondColumn3,Column3 from tab1
)
Select Column1,( CASE WHEN CHARINDEX(FirstColumn2 ,column3,1)> 0 then (CASE WHEN CHARINDEX(SecondColumn2 ,column3,1)> 0 then '' else SecondColumn2  + ' Deleted' end ) else FirstColumn2 + ' Deleted ' end ) 
+ ' and ' + (CASE WHEN CHARINDEX(FirstColumn3 ,column2,1)> 0 then (CASE WHEN CHARINDEX(SecondColumn3 ,column2,1)> 0 then '' else SecondColumn3  + ' Added' end ) else FirstColumn3 + ' Added ' end)  as Result   ,Column2,column3 ,CHARINDEX(FirstColumn2 ,column3,1)from TEst_CTE
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900