Replace String without Using EReplace Function

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
yugee
Participant
Posts: 34
Joined: Fri Feb 04, 2011 5:54 pm

Replace String without Using EReplace Function

Post by yugee »

Hi,

Thought of sharing a solution I have built to replace a sub-string in the string (similar to EReplace), but it is not a function.

(it will work only for the single occurance - I will post the solution for multiple occurance as well)
Input: I have ID and DESCRIPTION fields and Description has invalid string ' ' I want to replace it with ' ' (space)

1. To replace the single occurance
Transformer Stage:

Stage Variables:
VariableName:svStartPos
Initial Value: 0
SQLType: Integer
Derivation: Index(DSLink3.DESCRIPTION, " ", 1)

VariableName: svTotalLength
Initial Value: 0
SQLType: Integer
Derivation: Len(DSLink3.DESCRIPTION)

VariableName: svDescription
SQLType: VarChar (length should be same as your input column)

Derivation:

If svStartPos >0 Then
DSLink3.DESCRIPTION[1, svStartPos - 1] : ' ' : DSLink3.DESCRIPTION[svStartPos+6, svTotalLength - (svStartPos + 6 -1)]
Else
DSLink3.DESCRIPTION

---6 is length of my substring I want to repace


Output:
Map the svDescription to output
yugee
Participant
Posts: 34
Joined: Fri Feb 04, 2011 5:54 pm

Post by yugee »

Remove invalid characters or replace string with another - when it has multiple occurrences...

Input: DSLink3.Description
Invalid String: &nbsp
I want to replace it with Space: ' '

--Stage Variables:
VariableName:svStartPos
Initial Value: 0
SQLType: Integer
Derivation: Index(DSLink3.DESCRIPTION, " ", 1)

VariableName: svTotalLength
Initial Value: 0
SQLType: Integer
Derivation: Len(DSLink3.DESCRIPTION)

VariableName: svDescription
SQLType: VarChar (length should be same as your input column)

Derivation:

if svCount=0 then
DSLink3.DESCRIPTION
Else svDescription

VariableName-svCount
Initial Value: 0
SQLType: Integer
Derivation: count(DSLink3.DESCRIPTION,' ')

--Loop Condition

Loop While: @ITERATION <=svCount + 1

Loop Variable:

lpDescription:
If lpCount=0 then DSLink3.DESCRIPTION Else
lpDescription[1, lpStartPos - 1] : ' ' : lpDescription[lpStartPos+6, svTotalLength - (lpStartPos + 6 -1)]

lpStartPos: Index(lpDescription, "&nbsp;", 1)
lpCount: If lpCount=0 then svCount Else lpCount - 1


--Output:

Constraint: lpCount=0

Map the lpDescription to output
Post Reply