tarkan boy
tarkan boy

Reputation: 37

how to prepend a string before a substring in T-SQL?

If I have the following varchar:

SET @certificate = 'Custom_Birth-Certificates' 

and I want to prepend the string '_CA' before the -Certificates string by identifying the -Certificates string within the whole string, so the result string is for example 'Custom_Birth_CA-Certificates' ( the _CA string is prepend before -Certificates ),

so in a few words I want to identify the -Certificates word within the string an prepend another string.

How can I achieve that in T-SQL ?

Upvotes: 0

Views: 181

Answers (2)

JC Ford
JC Ford

Reputation: 7066

set @certificate = stuff(@certificate,patindex('%-Certificates',@certificate),0,'_CA')

Upvotes: 2

Tim Lehner
Tim Lehner

Reputation: 15261

One possibility, using REPLACE:

SET @certificate = REPLACE(@certificate, '-Certificates', '_CA' + '-Certificates')

This would replace all matches in the string.

Upvotes: 3

Related Questions