In SQL Server, you can use the LEFT()
function to extract the left part of a string.
It works exactly the same as the RIGHT()
function (which returns the right part of a string), except that it returns the left part of the string.
Example
Here’s an example to demonstrate.
SELECT LEFT('Catatonia', 3);
Result:
Cat
The first argument can be a constant, variable, or column. It can be of any data type, except text or ntext, that can be implicitly converted to varchar or nvarchar.
If it can’t be implicitly converted to varchar or nvarchar, use CAST()
or CONVERT()
to convert it to a supported data type.
The function returns varchar if you pass in a non-Unicode character data type.
It returns nvarchar if you pass a Unicode character data type.
Database Column Example
Here’s an example of extracting the left part of a value returned in a database column.
SELECT TOP(10)
name,
LEFT(name, 5)
FROM sys.objects;
Result:
+----------------+-------------+ | name | Left Part | |----------------+-------------| | sysrscols | sysrs | | sysrowsets | sysro | | sysclones | syscl | | sysallocunits | sysal | | sysfiles1 | sysfi | | sysseobjvalues | sysse | | sysmatrixages | sysma | | syspriorities | syspr | | sysdbfrag | sysdb | | sysfgfrag | sysfg | +----------------+-------------+