SQL Server: I would like to create a function that removes specific characters from a string, based on parameters.
- parameter1 is original string
- parameter2 is characters want to removed from original string
For example :
call MyRemoveFunc('32.87.65.54.89', '87.65' ) -- this will return '32.54.89'
call MyRemoveFunc('11.23.45', '23' ) -- this will return '11.45'
call MyRemoveFunc('14.99.16.84', '84.14' ) -- this will return '99.16'
call MyRemoveFunc('11.23.45.65.31.90', '23' ) -- this will return 11.45.65.31.90
call MyRemoveFunc('34.35.36', '35' ) -- this will return 34.36
call MyRemoveFunc('34.35.36.76.44.22', '35' ) -- this will return 34.36.76.44.22
call MyRemoveFunc('34', '34' ) -- this will return blank
call MyRemoveFunc('45.23.11', '45.11' ) -- this will return 23
Thanks
Here is one way using
Recursive CTE
andSplit string
functionResult :
The above code can be converted to a user defined function. I will suggest to create Inline Table valued function instead of Scalar function if you have more records.
Split string Function code referred from http://www.sqlservercentral.com/articles/Tally+Table/72993/
You could try this..