I'm looking for a inline table valued function that decodes URL encoded characters (like %20 for space). reference
I see other functions but haven't seen one that is an inline table valued function. I'm tempted in just writing a bunch of REPLACE statements in a function but I'm wondering if there's a better way.
CREATE FUNCTION dbo.itvfURLDecode
(
@StringValue VARCHAR(MAX)
)
RETURNS TABLE
AS
RETURN (
SELECT REPLACE(REPLACE(@StringValue,'%2B','+'),'%20',' ') /* etc..*/ AS DecodedValue
)
;WITH cteData AS
(
SELECT 'This%2Bis%20a%20test%3C%3E%20ok%3F%20100%25' AS example
)
SELECT
example, q.DecodedValue /* returns: This+is a test<> ok? 100% */
FROM cteData c
CROSS APPLY dbo.itvfURLDecode(example) q
Best Answer
While I'm not sure about the performance for you particular needs, here is an example of using a TranslationTable CTE along with a recursive CTE which will find and replace values in the incoming string to the TVF.
You can actually see the progression of the recursive CTE if you comment out the
--where cnt = 0
towards the bottom of the TVF