Remove CONVERT function from SQL query text using Regular expression

Viewed 19

I have a document to which I extracted the fields logic of an SQL query. From these logic lines I would like to remove CONVERT(TYPE, field_Name) function.

I have the following scenarios I need to address:

CONVERT(VARCHAR(50), pd.PartnerState),
CONVERT(DECIMAL(9, 2), pd.RiskRating),
CONVERT(NVARCHAR(200), REPLACE(REPLACE(REPLACE(pd.PartnerLegalName, '|', '/'), CHAR(10), ' '), CHAR(13), ' ')),
REPLACE(REPLACE(REPLACE(CONVERT(VARCHAR(150), pd.PartnerEmail), '|', '/'), CHAR(10), ' '), CHAR(13), ' '),

It's alright to address those scenarios separately (i.e. In different regular expressions) so long as a regular expression that handles one line won't compromise another.

The desired output would be:

pd.PartnerState,
pd.RiskRating,
REPLACE(REPLACE(REPLACE(pd.PartnerLegalName, '|', '/'), CHAR(10), ' '), CHAR(13), ' '),
REPLACE(REPLACE(REPLACE(pd.PartnerEmail, '|', '/'), CHAR(10), ' '), CHAR(13), ' '),

I'm using Python re.sub() so the removal part itself is not an issue but the actual identification of the groups that need to be removed.

So far I'm nowhere near tackling scenario number 4. (Where the CONVERT is nested in other functions). And what I've came up with: (^CONVERT\(.*?,)(.*)(\)) compromises line number 2.

Any help or tips would be greatly appreciated. Perhaps there's a smarter way to go about it using some Python library I'm not familiar with.

0 Answers
Related