Compare 2 strings in transformer

Post questions here relative to DataStage Enterprise/PX Edition for such areas as Parallel job design, Parallel datasets, BuildOps, Wrappers, etc.

Moderators: chulett, rschirm, roy

Post Reply
shalini11
Participant
Posts: 74
Joined: Thu Jan 22, 2009 3:00 am

Compare 2 strings in transformer

Post 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.
nani0907
Participant
Posts: 155
Joined: Wed Apr 18, 2007 10:30 am

Post 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.
thanks n regards
nani
shalini11
Participant
Posts: 74
Joined: Thu Jan 22, 2009 3:00 am

Post 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
adi_2_chaos
Participant
Posts: 96
Joined: Fri Apr 17, 2009 5:58 am
Location: Delhi

Re: Compare 2 strings in transformer

Post 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.
shalini11
Participant
Posts: 74
Joined: Thu Jan 22, 2009 3:00 am

Post 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.
shalini11
Participant
Posts: 74
Joined: Thu Jan 22, 2009 3:00 am

Post 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.
chulett
Charter Member
Charter Member
Posts: 43085
Joined: Tue Nov 12, 2002 4:34 pm
Location: Denver, CO

Post 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.
-craig

"You can never have too many knives" -- Logan Nine Fingers
Jasti
Participant
Posts: 44
Joined: Sat Apr 14, 2007 6:34 am
Location: Hyderabad, India

Re: Compare 2 strings in transformer

Post 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().
Thanks,
Mohan.A.Jasti.
pjangiti
Premium Member
Premium Member
Posts: 9
Joined: Wed Apr 09, 2008 5:57 am
Location: Hyderabad

Post by pjangiti »

Use Compare(Arg1, Arg2) function , the function returns 0 if both the arguments are similar
shalini11
Participant
Posts: 74
Joined: Thu Jan 22, 2009 3:00 am

Post 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
chulett
Charter Member
Charter Member
Posts: 43085
Joined: Tue Nov 12, 2002 4:34 pm
Location: Denver, CO

Post 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.
-craig

"You can never have too many knives" -- Logan Nine Fingers
shalini11
Participant
Posts: 74
Joined: Thu Jan 22, 2009 3:00 am

Post by shalini11 »

As per the requirement, it will be fine for me
Post Reply