SQL SUBSTRING

SQL SUBSTRING function is used to get part of a string. Different databases have different writtings:

SQL Server SUBSTRING()
Oracle SUBSTR()
MySQL SUBSTR(), SUBSTRING()

SQL SUBSTRING Syntax

SUBSTRING (str, pos, len)

Return a string start from pos and length is len.

or (this syntax is not supported by SQL Server):

SUBSTRING (str, pos)

Return a string start from pos and all the remaining characters.

SQL SUBSTRING Example

Table: Employees

EmployeeId FirstName LastName Department Salary
203 Bikliaa Fxoj Finance 78000
204 Jozzh Juheeo Finance 45800
205 Syllauu Dfaafk Finance 57000
206 Gecrrcc Srlkrt Finance 62000
302 Uifaou Bdnaa Development 75000
303 Nhuiaa Shillloa Development 55000
304 Yhadd Blooo Development 49000

Select all the employees with initial and last name:

SELECT SUBSTRING(FirstName, 1, 1) AS Initial, Lastname
FROM Employees

The result will look like:

Initial LastName
B Fxoj
J Juheeo
S Dfaafk
G Srlkrt
U Bdnaa
N Shillloa
Y Blooo

Leave a Comment