SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 Script Library
 URL decoding
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

SwePeso
Patron Saint of Lost Yaks

Sweden
30102 Posts

Posted - 09/05/2007 :  15:11:27  Show Profile  Visit SwePeso's Homepage  Reply with Quote
CREATE FUNCTION dbo.fnDeURL
(
	@URL VARCHAR(8000)
)
RETURNS VARCHAR(8000)
AS
BEGIN
	DECLARE @Position INT,
		@Base CHAR(16),
		@High TINYINT,
		@Low TINYINT,
		@Pattern CHAR(21)

	SELECT	@Base = '0123456789abcdef',
		@Pattern = '%[%][0-9a-f][0-9a-f]%',
		@Position = PATINDEX(@Pattern, @URL)

	WHILE @Position > 0
		SELECT	@High = CHARINDEX(SUBSTRING(@URL, @Position + 1, 1), @Base),
			@Low = CHARINDEX(SUBSTRING(@URL, @Position + 2, 1), @Base),
			@URL = STUFF(@URL, @Position, 3, CHAR(16 * @High + @Low - 17)),
			@Position = PATINDEX(@Pattern, @URL)

	RETURN	REPLACE(@URL, '+', ' ')
END


E 12°55'05.25"
N 56°04'39.16"

EDIT: Decoding + as Jeff mentioned

Edited by - SwePeso on 09/05/2007 16:04:12

jsmith8858
Dr. Cross Join

USA
7423 Posts

Posted - 09/05/2007 :  15:59:29  Show Profile  Visit jsmith8858's Homepage  Reply with Quote
Your subject says "URL Encoding" but the function does "URL Decoding".

Looks good, nice and short!

FYI -- URL Decoding should also decode + to a space.



- Jeff
http://weblogs.sqlteam.com/JeffS
Go to Top of Page

jsmith8858
Dr. Cross Join

USA
7423 Posts

Posted - 09/05/2007 :  16:08:19  Show Profile  Visit jsmith8858's Homepage  Reply with Quote
Perfect!





- Jeff
http://weblogs.sqlteam.com/JeffS
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

Sweden
30102 Posts

Posted - 09/05/2007 :  16:13:22  Show Profile  Visit SwePeso's Homepage  Reply with Quote
Thank you.

Now both + and %20 returns a single space.



E 12°55'05.25"
N 56°04'39.16"
Go to Top of Page

Kristen
Test

United Kingdom
22403 Posts

Posted - 09/06/2007 :  05:54:08  Show Profile  Reply with Quote
Here's my rather crappy EncodeURL function

CREATE FUNCTION dbo.fnEncodeURL
(
	@strInput	varchar(8000)	-- Note: String will get longer, so may overrun 8,000 characters
)
RETURNS varchar(8000)
/* WITH ENCRYPTION */
AS
/*
 * fnEncodeURL	Encode value to be used as URL (i.e. substitute reserved characters with %nn)
 *
 *	SELECT dbo.fnEncodeURL(MyColumnToURLEncode)
 *
 * Returns:
 *
 *	Encoded Content 
 *
 * HISTORY:
 *
 * 19-Jun-2004 KBM  Started
 */
BEGIN
	IF @strInput IS NULL
	BEGIN
		-- Just return NULL if input string IS NULL
		RETURN NULL
	END
	SELECT @strInput=
			REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(
			REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(
			REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(
			REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(
			REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(
			REPLACE(REPLACE(
					@strInput,
			    			'%', '%25'),	-- Must be inner-most replace
						CHAR(10), '%0A'),
						CHAR(13), '%0D'),
	      					' ', '%20'),
			    			':', '%3A'),
			    			';', '%3B'),
			    			'-', '%2D'),
			    			'/', '%2F'),
			    			'\', '%5C'),
			    			'!', '%21'),
			    			'"', '%22'),
			    			'#', '%23'),
			    			'?', '%3F'),
			    			'=', '%3D'),
			    			'@', '%40'),
--			    			'%', '%25'),
			    			'>', '%3E'),
			    			'<', '%3C'),
			    			'$', '%24'),
			    			'&', '%26'),
			    			'[', '%5B'),
			    			']', '%5D'),
			    			'~', '%7E'),
			    			'^', '%5E'),
			    			'`', '%60'),
			    			'{', '%7B'),
			    			'}', '%7D'),
			    			'|', '%7C')


	RETURN @strInput
END
/* TEST RIG


-- Code Example

	SELECT dbo.fnEncodeURL('8ECEE9BE-05BD-4DD2-9D09-6C121E0924E7')

 */
--================== fnEncodeURL ==================--
GO

Kristen

Edited by - Kristen on 09/06/2007 10:01:14
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

Sweden
30102 Posts

Posted - 09/06/2007 :  07:17:39  Show Profile  Visit SwePeso's Homepage  Reply with Quote
I think you need % for the inner most REPLACE

SELECT dbo.fnEncodeURL('Peso:Kristen') returns "Peso%253AKristen"

ALTER FUNCTION dbo.fnEncodeURL
(
	@strInput	varchar(8000)
)
RETURNS varchar(8000)
AS
BEGIN
	RETURN
			REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(
			REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(
			REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(
			REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(
			REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(
			REPLACE(REPLACE(
					@strInput,
			    			'%', '%25'),
						CHAR(10), '%0A'),
						CHAR(13), '%0D'),
	      					' ', '%20'),
			    			':', '%3A'),
			    			';', '%3B'),
			    			'-', '%2D'),
			    			'/', '%2F'),
			    			'\', '%5C'),
			    			'!', '%21'),
			    			'"', '%22'),
			    			'#', '%23'),
			    			'?', '%3F'),
			    			'=', '%3D'),
			    			'@', '%40'),
			    			'>', '%3E'),
			    			'<', '%3C'),
			    			'$', '%24'),
			    			'&', '%26'),
			    			'[', '%5B'),
			    			']', '%5D'),
			    			'~', '%7E'),
			    			'^', '%5E'),
			    			'`', '%60'),
			    			'{', '%7B'),
			    			'}', '%7D'),
			    			'|', '%7C')
END
Now SELECT dbo.fnEncodeURL('Peso:Kristen') returns "Peso%3AKristen"



E 12°55'05.25"
N 56°04'39.16"
Go to Top of Page

Kristen
Test

United Kingdom
22403 Posts

Posted - 09/06/2007 :  09:57:04  Show Profile  Reply with Quote
"I think you need % for the inner most REPLACE"

Yeah, you would be right.

I'm please to report that the QA Tester for that routine was shot at dawn!
Go to Top of Page

jsmith8858
Dr. Cross Join

USA
7423 Posts

Posted - 09/06/2007 :  10:19:03  Show Profile  Visit jsmith8858's Homepage  Reply with Quote
Hey Peso -- Just noticed that you did your REPLACE too late for the + to SPACE. %2B is the plus sign, but if you run:

select dbo.fnDeURL('jeff%2Bsmith')

instead of

jeff+smith

you get:

jeff smith



- Jeff
http://weblogs.sqlteam.com/JeffS
Go to Top of Page

Kristen
Test

United Kingdom
22403 Posts

Posted - 09/06/2007 :  10:27:08  Show Profile  Reply with Quote
Hahaha .. the boot's on the other foot!

Kristen
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

Sweden
30102 Posts

Posted - 09/07/2007 :  08:12:03  Show Profile  Visit SwePeso's Homepage  Reply with Quote
CREATE FUNCTION dbo.fnDeURL
(
	@URL VARCHAR(8000)
)
RETURNS VARCHAR(8000)
AS
BEGIN
	DECLARE @Position INT,
		@Base CHAR(16),
		@High TINYINT,
		@Low TINYINT,
		@Pattern CHAR(21)

	SELECT	@Base = '0123456789abcdef',
		@Pattern = '%[%][0-9a-f][0-9a-f]%',
		@URL = REPLACE(@URL, '+', ' '),
		@Position = PATINDEX(@Pattern, @URL)

	WHILE @Position > 0
		SELECT	@High = CHARINDEX(SUBSTRING(@URL, @Position + 1, 1), @Base),
			@Low = CHARINDEX(SUBSTRING(@URL, @Position + 2, 1), @Base),
			@URL = STUFF(@URL, @Position, 3, CHAR(16 * @High + @Low - 17)),
			@Position = PATINDEX(@Pattern, @URL)

		RETURN	@URL
END



E 12°55'05.25"
N 56°04'39.16"
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.08 seconds. Powered By: Snitz Forums 2000