Page 1 of 1

Compare 2 strings in transformer

Posted: Tue Apr 28, 2009 5:43 am
by shalini11
Hi All,

I have a requirement to compare 2 columns and if they are same then print the 1st one. If both are different, then print the 1st concatenated with the 2nd.

Eg: The 2 columns are:
Col1 Col2
Air Air
Water Air

In the 1st case Col1=Col2, so the output should be Air
In the 2nd case Col1<>Col2, so the output should be WaterAir


In transformer, I am used If Then Else condition to do this, but it is concatenating both the columns irrespective of the condition.

Could you pls help.

Posted: Tue Apr 28, 2009 6:00 am
by nani0907
hi

use below condition in stage variable

if trim(col1)= trim(col2) then col1 else if trim(col1)<>trim(col2) then col:col2 else stagevariable name.

Posted: Tue Apr 28, 2009 6:10 am
by shalini11
Thanks for the reply

I am using the same condition.The columns are getting concatenated even if they are same. They should be concatenated only if they are different

Re: Compare 2 strings in transformer

Posted: Tue Apr 28, 2009 6:16 am
by adi_2_chaos
Hi,

You may try the following derivation:
If (DSLink2.ID1 eq DSLink2.ID2) Then DSLink2.ID1 Else DSLink2.ID1 : DSLink2.ID2.

This should work.
Shri

shalini11 wrote:Hi All,

I have a requirement to compare 2 columns and if they are same then print the 1st one. If both are different, then print the 1st concatenated with the 2nd.

Eg: The 2 columns are:
Col1 Col2
Air Air
Water Air

In the 1st case Col1=Col2, so the output should be Air
In the 2nd case Col1<>Col2, so the output should be WaterAir


In transformer, I am used If Then Else condition to do this, but it is concatenating both the columns irrespective of the condition.

Could you pls help.

Posted: Tue Apr 28, 2009 6:26 am
by shalini11
One thing I forgot to tell the strings are having spaces in between.

Eg: Air Conditioner

But both the column values are same with the spaces also at the same position. Then also it is considering them as different and concatenating.

Posted: Tue Apr 28, 2009 6:32 am
by shalini11
If I use single words as column values, then the condition is working fine. But when I use values like 'Air Conditioner', 'AC Service', then transformer treats them as different.

How can i remove the spaces in between the values.

Posted: Tue Apr 28, 2009 6:35 am
by chulett
Spaces are just another character and will match just fine. Post your exact derivations so we can see where you are going wrong.

Question: is it considered a match if one is "Air.Conditioner" with one space between words and the other is "Air..Conditioner" with two spaces? Or if one has no trailing spaces and the other does? I ask because that will control how you trim the fields before the comparison.

Re: Compare 2 strings in transformer

Posted: Tue Apr 28, 2009 6:35 am
by Jasti
[quote="adi_2_chaos"]Hi,

You may try the following derivation:
If (DSLink2.ID1 eq DSLink2.ID2) Then DSLink2.ID1 Else DSLink2.ID1 : DSLink2.ID2.
Hi adi_2_chaos,

What is eq in If (DSLink2.ID1 eq DSLink2.ID2)

Shalini,
Use

Code: Select all

If (trim(col1)= trim(col2)) Then (col1) Else (col1:col2)
directly as derivation for the output column.
Trim() will remove the leading and trailing spaces in a string. If you want to remove all the spaces in a string, you will have to use StripWhiteSpace().

Posted: Tue Apr 28, 2009 6:43 am
by pjangiti
Use Compare(Arg1, Arg2) function , the function returns 0 if both the arguments are similar

Posted: Tue Apr 28, 2009 6:43 am
by shalini11
Thanks Mohan,

I used StripWhiteSpaces() and it worked fine.

If StripWhiteSpace(Col1)<>StripWhiteSpace(Col2) Then Trim(Col1) : " ":Trim(Col2) Else Trim(Col1)

Thank you all

Posted: Tue Apr 28, 2009 7:02 am
by chulett
Just keep in mind the fact that if you remove all whitespace, it will match "A BC" with "AB C"... only you know if that is fine or not.

Posted: Tue Apr 28, 2009 7:26 am
by shalini11
As per the requirement, it will be fine for me