sql - Table driven nested function calls -
sql - Table driven nested function calls -
i'm looking way apply arbitrary number of nested replace calls on column. ideally pattern , replace values come configuration table, e.g.:
╔═════════╦═════════╗ ║ pattern ║ replace ║ ╠═════════╬═════════╣ ║ -jan- ║ /01/ ║ ║ -feb- ║ /02/ ║ ║ -mar- ║ /03/ ║ ║ -apr- ║ /04/ ║ ║ -may- ║ /05/ ║ ║ etc. ║ ║ ╚═════════╩═════════╝
the question same this one, reply given works when assigning result of 1 input value @ time - i'd homecoming entire resultset.
i thinking recursive cte might work..
sample data
╔═══════════════╦══════════════╗ ║ input ║ output ║ ╠═══════════════╬══════════════╣ ║ datadata-jan- ║ datadata/01/ ║ ║ -apr--may- ║ /04//05/ ║ ║ -mar-testing ║ /03/testing ║ ╚═══════════════╩══════════════╝
take solution in link mentioned, , set in function:
create table replacement(replacethis varchar(10), withthis varchar(10)) insert replacement values ('-jan-', '/01/'), ('-feb-', '/02/'), ('-mar-', '/03/'), ('-apr-', '/04/'), ('-may-', '/05/') create table input(val varchar(100)) insert input values ('datadata-jan-'), ('-apr--may-'), ('-mar-testing') go create function dbo.replacevalues(@input varchar(100)) returns varchar(100) begin select @input = replace(@input, replacethis, isnull(withthis, '')) replacement charindex(replacethis, @input) > 0 homecoming @input end go select val, dbo.replacevalues(val) input
sql fiddle
sql sql-server sql-server-2008 tsql
Comments
Post a Comment