sql server - How to find the next 10 characters after a string -


i have string "create procedure test". please find requirement below:

1- have find string "procedure".

2- after want search if particular string "dbo" exists in next 5 characters after string procedure.

3- if dbo not exists want concatenate string myschema before string test.

any suggestions

a regular if simple calculations should need;

declare @input   nvarchar(max) = 'create procedure test' declare @keyword nvarchar(max) = 'procedure' declare @inject  nvarchar(max) = 'myschema.'  declare @index    bigint = charindex(@keyword, @input) declare @dboindex bigint = charindex('dbo', @input, @index)  if @dboindex < 1 or @dboindex - @index - len(@keyword) > 5 begin   set @input = stuff(@input, @index + len(@keyword) + 1, 0, @inject) end  select @input 

an sqlfiddle test with.

charindex find strings you're looking exist in original string, if positions differ 5 or less (or dbo not exist @ all), uses stuff inject new string @ fixed position after "procedure".

you may want add further tolerance go, example handles single space after "procedure".


Comments

Popular posts from this blog

jQuery Mobile app not scrolling in Firefox -

c++ - How to add Crypto++ library to Qt project -

php array slice every 2th rule -