SQL LEFT() FunctionFind Character or Substring Index Position from Start Location, Left Side, or Beginning of a String
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.
Related Links
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 |
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 |
Related Links