Skip to main content
Announcements
A fresh, new look for the Data Integration & Quality forums and navigation! Read more about what's changed.
cancel
Showing results for 
Search instead for 
Did you mean: 
welshsteve
Creator

Using StringHandling to replace forward slash / with backslash \

Hi. I am trying to design a job which takes input from a data table containing filepaths to documents

e.g.

0693p00000AZtgsAAD.png 

In the job I want to replace the relative path C:\1stdir32\ with nothing.

In SQL, I would simply run this:

UPDATE Documents

SET [Path] = REPLACE([Path], 'C:\1stdir32\', '')

However, I want to achieve this in a job with a context parameter that will prompt the user for a relative path at runtime. I would like this context parameter to be of type "Directory".

The issue with this though is Talend's insistence on using forward slashes instead of the standard Windows backslashes in paths (I know this is due to it being Java based.)

So when I set this up, the directory being passed to the context parameter at runtime is C:/1stdir32/ and not C:\1stdir32\.

This basically means nothing gets updated when the job runs.

So I'm seeing is there is a way I can replace the forward slashes in the context parameter to backslashes using the StringHandling function. But I'm not getting any joy.

I am executing the code via a tDBRow component. Here's my code:

UPDATE Documents

SET [Path] = REPLACE(CAST([Path] AS VARCHAR(MAX)), '" + StringHandling.CHANGE(context.parameter_path_relative, "/", "\") + "', '')

WHERE CAST([Path] AS VARCHAR(MAX)) LIKE '" +StringHandling.CHANGE(context.parameter_path_relative, "/", "\") + "%'

This doesn't work, so I tried escaping the backslash by doubling up on it, but still no joy.

UPDATE Documents

SET [Path] = REPLACE(CAST([Path] AS VARCHAR(MAX)), '" + StringHandling.CHANGE(context.parameter_path_relative, "/", "\\") + "', '')

WHERE CAST([Path] AS VARCHAR(MAX)) LIKE '" +StringHandling.CHANGE(context.parameter_path_relative, "/", "\\") + "%'

Labels (2)
1 Reply
Prakhar1
Creator III

context.parameter_path_relative = context.parameter_path_relative.replace( "/", "\\") 

 

Do this in tjavafex before running the DBOutput component