Tip of the Day : Query String Parser Function

Welcome to SQL Server Helper !!!

This site is intended for those who are beginning to use SQL Server as part of their day-to-day activities.  You will find in this site a collection of useful functions, triggers, stored procedures and tips and tricks related to SQL Server.

Should you have any comments or questions regarding this site or if you want to ask SQL Server-related questions, e-mail us here.

We hope we are able to help and we are glad to help!!!

SQL Server Tip of the Day - October 21, 2017

Query String Parser Function

A query string is the part of a URL that contains the data to be passed from the web browser and sent to the web application for processing. In a URL, the query string follows a separating character, usually a question mark (?). The query string is composed of a series of key-value pairs. Within each pair, the key and value are separated by an equals sign (=). The series of key-value pairs are typically separated by the ampersand (&) character.

Here’s a function that parses a query string and returns the key-value pairs as a table:

CREATE FUNCTION [dbo].[ufn_ParseQueryString] 
( @QueryString AS VARCHAR(MAX) )
RETURNS @QueryStringTable TABLE 
( [Key] VARCHAR(100), [Value] VARCHAR(1000) )
    DECLARE @QueryStringPair        VARCHAR(2000)
    DECLARE @Key                    VARCHAR(100)
    DECLARE @Value                  VARCHAR(1000)

    WHILE LEN(@QueryString) > 0
        SET @QueryStringPair = LEFT ( @QueryString, ISNULL(NULLIF(CHARINDEX('&', @QueryString) - 1, -1), 
        SET @QueryString = SUBSTRING( @QueryString, ISNULL(NULLIF(CHARINDEX('&', @QueryString), 0), 
                                      LEN(@QueryString)) + 1, LEN(@QueryString))

        SET @Key   = LEFT (@QueryStringPair, ISNULL(NULLIF(CHARINDEX('=', @QueryStringPair) - 1, -1), 
        SET @Value = SUBSTRING( @QueryStringPair, ISNULL(NULLIF(CHARINDEX('=', @QueryStringPair), 0), 
                                LEN(@QueryStringPair)) + 1, LEN(@QueryStringPair))

        INSERT INTO @QueryStringTable ( [Key], [Value] )
        VALUES ( @Key, @Value )


And here’s a sample query that calls this function and outputs the query string key-value pairs as a table:

SELECT * FROM [dbo].[ufn_ParseQueryString] ( 'fname=Barack&lname=Obama&addr=1600 Pennsylvania Ave NW
&city=Washington&st=DC&zip=20500' )
Key     Value
------- ----------------------------
fname   Barack
lname   Obama
addr    1600 Pennsylvania Ave NW
city    Washington
st      DC
zip     20500

SQL Server 2012

SQL Server 2008

User-Defined Functions

Date Functions

A collection of useful user-defined functions that deal with dates.

String Functions

A collection of useful user-defined functions that deal with strings (varchar/char/nvarchar/nchar).

Tree Functions

A collection of useful user-defined functions that deal with tree or hierarchical design structures.

Table-Valued Functions

A collection of useful table-valued user-defined functions that can be used to join with other tables.

SQL Server Built-in Functions

A reference to all built-in functions available within SQL Server grouped into categories.

Tips and Tricks

A collection of useful SQL Server-related tips and tricks:

SQL Server Error Messages

A list of SQL Server error messages and for certain error messages, discusses ways on how to solve the error or work around them:

Frequently Asked Questions