Web functions

chm.HtmlEncode

Converts given argument into an HTML-encoded string.

		  CREATE FUNCTION [chm].[HtmlEncode]
(
	@data NVARCHAR(MAX)
)
RETURNS NVARCHAR(MAX)
		

Example:

		  print chm.HtmlEncode('<b>Jacky & Jill</b>')
	
		

Output:

		  &lt;b&gt;Jacky &amp; Jill&lt;/b&gt;
	
		

chm.HtmlDecode

Decodes a an HTML-encoded string.

		  CREATE FUNCTION [chm].[HtmlDecode]
(
	@data NVARCHAR(MAX)
)
RETURNS NVARCHAR(MAX)
		

Example:

		  print chm.HtmlDecode('&lt;b&gt;Jacky &amp; Jill&lt;/b&gt;')
		

Output:

		  <b>Jacky & Jill</b>
	
		

chm.UrlEncode

Encodes URL characters in the given argument to their equivalent characters that are allowed in URLs. It encodes characters after first occurance of question mark character. So, it is useful to encode a url where we want only its querystring is encoded, not the whole URL.

		  CREATE FUNCTION [chm].[UrlEncode]
(
	@data NVARCHAR(MAX)
)
RETURNS NVARCHAR(MAX)
		

Example:

		  print chm.UrlEncode('http://a.com/b/?name=john doe&category=C++')
		

Output:

		  http://a.com/b/?name=john+doe&category=C%2B%2B
		

chm.FullUrlEncode

Encodes URL characters in the given argument to their equivalent characters. It encodes entire string. So, it is useful to encode a value whatever it is (either be a URL) that is going to be sent through querystring.

		  CREATE FUNCTION [chm].[FullUrlEncode]
(
	@data NVARCHAR(MAX)
)
RETURNS NVARCHAR(MAX)
		

Example:

		  select chm.FullUrlEncode('http://a.com/b/?name=john doe&category=C++')
		

Output:

		  http%3A%2F%2Fa.com%2Fb%2F%3Fname%3Djohn+doe%26category%3DC%2B%2B
		

chm.UrlDecode

Decodes an already URL encoded argument. It decodes characters only after first occurance of question mark. So, it is useful to decode a url where we want only its querystring to be decoded, not the whole URL.

		  CREATE FUNCTION [chm].[UrlDecode]
(
	@data NVARCHAR(MAX)
)
RETURNS NVARCHAR(MAX)
		

Example:

		  select chm.UrlDecode('http://a.com/b/?name=john+doe&category=C%2B%2B')
		

Output:

		  http://a.com/b/?name=john doe&category=C++
		

chm.FullUrlDecode

Decodes an already URL encoded argument. It decodes the entire argument. So, it is useful to decode a value received through querystring and make sure it is completely decoded.

		  CREATE FUNCTION [chm].[FullUrlDecode]
(
	@data NVARCHAR(MAX)
)
RETURNS NVARCHAR(MAX)
		

Example:

		  select chm.FullUrlDecode('http%3A%2F%2Fa.com%2Fb%2F%3Fname%3Djohn+doe%26category%3DC%2B%2B')
		

Output:

		  http://a.com/b/?name=john doe&category=C++
		

chm.JsonEncode

Encodes given argument in a way it conforms to a valid JSON string.

		  CREATE FUNCTION [chm].[JsonEncode]
(
	@data NVARCHAR(MAX)
)
RETURNS NVARCHAR(MAX)
		

Example:

		  select chm.JsonEncode('Quote: "To be or not to be"')    -- Quote: \"To be or not to be\"
select chm.JsonEncode('10 \ 5 = 2')                     -- 10 \\ 5 = 2
		

This UDF is useful wehn we want to make a JSON string manually and safely put a string value inside it as say, the value of a property.

Example:

		  declare @json varchar(500)
declare @name varchar(50)

-- Not Safe
set @json = '{ "name": "' + @name + '" }'   -- this is prone to error if @name contains
											-- characters such as " or \
-- Safe
set @json = '{ "name": "' + chm.JsonEncode(@name) + '" }'
		

chm.JsonDecode

Decodes an already JSON encoded string.

		  CREATE FUNCTION [chm].[JsonDecode]
(
	@data NVARCHAR(MAX)
)
RETURNS NVARCHAR(MAX)
		

Example:

		  select chm.JsonDecode('Quote: \"To be or not to be\"')  -- Quote: "To be or not to be"
select chm.JsonDecode('10 \\ 5 = 2')                    -- 10 \ 5 = 2