Export (0) Print
Expand All
Expand Minimize
This topic has not yet been rated - Rate this topic

Returns part of a character, binary, text, or image expression.



SUBSTRING ( expression, start, length ) 
expression

A character string, binary string, text, image, a column, or an expression that includes a column. Do not use expressions that include aggregate functions.

start

An integer or an expression that can be implicitly converted to int, which specifies where the substring begins.

length

An integer or an expression that can be implicitly converted to int, which specifies the length of the substring.


Returns character data if expressionis one of the supported character data types. Returns binary data if expressionis one of the supported binary data types. If start = 1, then the substring begins from the first character of the expression.

The returned string is the same type as the given expression with the exceptions shown in the following table.

Given expression

Return type

image

varbinary

ntext

nvarchar

The following example returns the initial of the first name and the full last name for each employee in the Employees table.

SELECT SUBSTRING([First Name],1,1) AS Initial, [Last Name]
FROM Employees

This is the result set:


Initial            Last Name
----------------------------
N           Davolio
A           Fuller
J           Leverling
M           Peacock
S           Buchanan
M           Suyama
R           King
L           Callahan
A           Dodsworth
A           Hellstern
T           Smith
C           Patterson
J           Brid
X           Martin
L           Pereira
(15 rows affected)
Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft. All rights reserved.