Skip Navigation Links
Tips & Tricks
SQL Server 2005
SQL Server 2008
SQL Server 2012
Practice Test
Tip of the Day : Example Uses of the SUBSTRING String 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 - September 30, 2014
Example Uses of the SUBSTRING String Function

The SUBSTRING string function returns part of a character, binary, text or image expression.  The syntax of the SUBSTRING string function is as follows:

SUBSTRING( <expression>, <start_position>, <length> )

The <expression> parameter is a character string, binary string, text, image, a column or an expression that includes a column.  The <start_position> parameter is an integer that specifies where the substring starts and can be of BIGINT data type.  The <length> parameter is a positive integer that specifies how many characters or bytes of the <expression> will be returned.

Here are a few uses of the SUBSTRING string function:

Usage #1 : Get the First Name and Last Name from a Full Name

DECLARE @FullName    VARCHAR(50) = 'Mark Zuckerberg'

SELECT SUBSTRING(@FullName, 1, CHARINDEX(' ', @FullName) - 1) AS [First Name],
       SUBSTRING(@FullName, CHARINDEX(' ', @FullName) + 1, LEN(@FullName)) AS [Last Name]
First Name    Last Name
------------  ------------
Mark          Zuckerberg

Usage #2 : Get the First and Last Word from a String or Sentence

DECLARE @Sentence    VARCHAR(MAX) = 'The quick brown fox jumps over the lazy dog'

SELECT SUBSTRING(@Sentence, 1, CHARINDEX(' ', @Sentence) - 1) AS [First Word],
       REVERSE(SUBSTRING(REVERSE(@Sentence), 1, 
CHARINDEX(' ', REVERSE(@Sentence)) - 1)) AS [Last Word]
First Word   Last Word
------------ -----------
The          dog

Usage #3 : Get the Domain Name, Page Name and Query Parameter from a URL

SET @URL = ''

SELECT SUBSTRING(@URL, 8, CHARINDEX('/', @URL, 9) - 8) AS [Domain Name],
       CHARINDEX('/', REVERSE(@URL)) - CHARINDEX('?', REVERSE(@URL)) - 1)) AS [Page Name],
       SUBSTRING(@URL, CHARINDEX('?', @URL) + 1, LEN(@URL)) AS [Query Parameter]
Domain Name                Page Name            Query Parameter
-------------------------- -------------------- -----------------  tip-of-the-day.aspx  tid=58

Usage #4 : Get the User Name and Domain Name from an Email Address

DECLARE @Email     VARCHAR(50) = ''

SELECT SUBSTRING(@Email, 1, CHARINDEX('@', @Email) - 1) AS [User Name],
       SUBSTRING(@Email, CHARINDEX('@', @Email) + 1, LEN(@Email)) AS [Domain Name]
User Name   Domain Name
----------- -------------

Usage #5 : Convert a Comma-Delimited List to a Table

DECLARE @StringInput  VARCHAR(100) = 'Monday,Tuesday,Wednesday'
DECLARE @StringValue  VARCHAR(100)
DECLARE @OutputTable TABLE (
    [StringValue]     VARCHAR(100)

WHILE LEN(@StringInput) > 0
    SET @StringValue = LEFT(@StringInput, 
                            ISNULL(NULLIF(CHARINDEX(',', @StringInput) - 1, -1),
    SET @StringInput = SUBSTRING(@StringInput,
                                 ISNULL(NULLIF(CHARINDEX(',', @StringInput), 0),
                                 LEN(@StringInput)) + 1, LEN(@StringInput))

    INSERT INTO @OutputTable ( [StringValue] )
    VALUES ( @StringValue )

SELECT * FROM @OutputTable

Usage #6 : Convert a Name/Value Pair List to a Table

DECLARE @NameValuePairs  VARCHAR(MAX) = 'Color=White;BackColor=DarkBlue;Font=Arial'
DECLARE @NameValuePair   VARCHAR(100)
DECLARE @Name            VARCHAR(50)
DECLARE @Value           VARCHAR(50)
    [Name]               VARCHAR(50),
    [Value]              VARCHAR(50)

WHILE LEN(@NameValuePairs) > 0
    SET @NameValuePair = LEFT(@NameValuePairs, 
                              ISNULL(NULLIF(CHARINDEX(';', @NameValuePairs) - 1, -1),
    SET @NameValuePairs = SUBSTRING(@NameValuePairs,
                                    ISNULL(NULLIF(CHARINDEX(';', @NameValuePairs), 0),
                                    LEN(@NameValuePairs)) + 1, LEN(@NameValuePairs))

    SET @Name = SUBSTRING(@NameValuePair, 1, CHARINDEX('=', @NameValuePair) - 1)
    SET @Value = SUBSTRING(@NameValuePair, CHARINDEX('=', @NameValuePair) + 1, LEN(@NameValuePair))

    INSERT INTO @Property ( [Name], [Value] )
    VALUES ( @Name, @Value )

SELECT * FROM @Property
Name        Value
----------- ---
Color       White
BackColor   DarkBlue
Font        Arial
SQL Server 2012
What's New in SQL Server 2012
SQL Server 2012 Articles
SQL Server 2008
What's New in SQL Server 2008

A collection of articles related to what's new with SQL Server 2008:

SQL Server 2008 Articles

A collection of articles related to 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
SQL Server

A collection of frequently asked questions about SQL Server grouped by categories:

SQL Server 2005

A collection of frequently asked questions about SQL Server 2005 grouped by categories: