LEFT function in Oracle
Mastering the LEFT Function in Oracle SQL

Explore the LEFT function in Oracle SQL, its syntax, common use cases, and how to achieve similar functionality using SUBSTR for efficient string manipulation.
While many SQL dialects, like MySQL and SQL Server, offer a direct LEFT()
function, Oracle SQL does not have a native LEFT()
function. However, Oracle provides the powerful SUBSTR()
function, which can easily replicate the functionality of LEFT()
. This article will guide you through understanding how to extract characters from the beginning of a string in Oracle SQL, focusing on SUBSTR()
and its practical applications.
Understanding Oracle's Approach: The SUBSTR Function
In Oracle, the SUBSTR()
function is a versatile tool for extracting substrings from a given string. To mimic the behavior of a LEFT()
function, you simply need to specify the starting position as 1 and the desired length. This allows you to retrieve a specified number of characters from the left-hand side of a string.
SUBSTR(string, start_position, length)
Generic syntax for the SUBSTR function in Oracle SQL.
To get the equivalent of LEFT(string, N)
, you would use SUBSTR(string, 1, N)
.
Practical Examples of Using SUBSTR for LEFT Functionality
Let's look at some common scenarios where you might need to extract characters from the beginning of a string and how to achieve this using SUBSTR()
in Oracle.
-- Example 1: Extracting the first 5 characters from a string
SELECT SUBSTR('Oracle Database', 1, 5) AS first_five_chars
FROM DUAL;
-- Result: 'Oracl'
-- Example 2: Extracting the first 7 characters from a column
SELECT product_name,
SUBSTR(product_name, 1, 7) AS short_product_name
FROM products
WHERE product_id = 101;
-- Example 3: Handling strings shorter than the requested length
SELECT SUBSTR('SQL', 1, 5) AS short_string_example
FROM DUAL;
-- Result: 'SQL' (SUBSTR returns the entire string if length exceeds it)
Demonstrating SUBSTR to replicate LEFT function behavior.
length
in SUBSTR(string, 1, length)
is greater than the actual length of the string
, Oracle's SUBSTR
function will gracefully return the entire string without error, which is often the desired behavior for a LEFT
equivalent.Visualizing the SUBSTR Process
The following diagram illustrates how SUBSTR
extracts characters from the beginning of a string, effectively acting as a LEFT
function.
flowchart LR A["Input String: 'Hello World'"] --> B{"SUBSTR(string, 1, 5)"} B --> C["Start Position: 1"] B --> D["Length: 5"] C & D --> E["Output: 'Hello'"]
Flowchart illustrating how SUBSTR extracts characters from the left.
Advanced Use Cases and Considerations
Beyond simple extraction, SUBSTR
can be combined with other functions for more complex string manipulations. For instance, you might need to extract a portion of a string up to a certain delimiter, or dynamically determine the length to extract.
-- Example 4: Extracting the first word before a space
SELECT 'Oracle Database' AS original_string,
SUBSTR('Oracle Database', 1, INSTR('Oracle Database', ' ') - 1) AS first_word
FROM DUAL;
-- Result: 'Oracle'
-- Example 5: Extracting the domain from an email address
SELECT 'user@example.com' AS email_address,
SUBSTR('user@example.com', 1, INSTR('user@example.com', '@') - 1) AS username
FROM DUAL;
-- Result: 'user'
Combining SUBSTR with INSTR for dynamic left-side extraction.
INSTR()
function is crucial here, as it finds the position of a substring within a string. By subtracting 1, we ensure that the delimiter itself is not included in the extracted portion.