SQL LEFT() Function


The SQL LEFT() is a function, and extracts substring or number of characters from a given input string or text.

The SQL LEFT() function is extracts data or substring from left most character.

The SQL LEFT() function is supports or work with character and numeric based columns.

It can be used in any valid SQL SELECT statement as well in SQL where clause.


SQL LEFT() Syntax

The below syntax is used to extracts substring or number of characters from a given input string or text.


SELECT LEFT(string or text, number_of_characters);

The below syntax is used to extracts substring or number of characters from a given table column or field.


SELECT LEFT(column_name1, number_of_characters) FROM table_name;


SQL LEFT() Example - Using Expression Or Formula

The following SQL SELECT statement will extract number of characters of a given input string or text.


SELECT LEFT('How Are You', 3) AS 'Read 3 chars from left side';

The result of above query is:

Read 3 chars from left side
How

SQL LEFT() More Example

Input Value Result
Left('Hello', 2) He
Left('Hi, it's nice database tutorial!', 10) Hi, it's n
Left('12345', 3) 123

Sample Database Table - Books

BookId BookName BookPrice RelYear DomainName AuthorName
1 SQL All-in-One For Dummies 136.33 2015 Optimization Geetha
2 The Microsoft Data Warehouse 140 2006 Optimization Nirmala
3 MySql Interview Questions 168.27 2008 Administration Dharan
4 Oracle Cookbook 99.99 2012 Database Bala Murugan
5 The SQL Programming Language 155 2006 Security Padmavathi
6 PHP And MySQL Bible 200 2006 Database Vidyavathi
7 Introduction to SQL and PL/SQL 60 2013 Optimization Nirmala
8 How to Write Accurate SQL Code 71.87 2010 Database Rishi Keshan
9 From Access To SQL Server 165 2010 Administration Geetha
10 SQL for students 199.97 2011 Security Hanumanthan
11 Securing MySql 125 2014 Administration Dharan
12 SQL Server 2008 Transact-Sql 65 2012 Administration Rishi Keshan

SQL LEFT() Example - With Table Column

The following SQL statement selects the "BookID", "BookName" and "BookPrice" fields from the "Books" table, and extract the specified number of characters from "Books" table:


SELECT BookID,
LEFT(BookName, 6) As 'Book Name First 6 Char(s)', 
LEFT(BookPrice, 2) As 'Book Price First 2 Char(s)'
FROM Books;

The result of above query is:

BookID Book Name First 6 Char(s) Book Price First 2 Char(s)
1 SQL Al 13
2 The Mi 14
3 MySql 16
4 Oracle 99
5 The SQ 15
6 PHP An 20
7 Introd 60
8 How to 71
9 From A 16
10 SQL fo 19
11 Securi 12
12 SQL Se 65

Search Keys

  • sql find string
  • sql find in string
  • sql find string position
  • find in sql server
  • sql string search
  • sql character functions
  • sql first character
  • sql string functions
  • sql string functions examples
  • sql string functions with examples