<<<<<<< HEAD left | H2 | SQLDoc
Skip to main content

left

1. LEFT Function

======= left | H2 Function | SQLDoc

left

1. LEFT Function

>>>>>>> 9d19b1c8cb744c8e86d5c15b74d8b5f719ad62be

The LEFT function in H2 database is used to extract a specified number of characters from the beginning (leftmost) side of a given string.

2. Syntax

The syntax for the LEFT function in H2 database is as follows:

LEFT(str, length)

Arguments

  • str: The input string from which characters need to be extracted. It should be a string or a column of string data type.
  • length: The number of characters to be extracted from the left side of the string. It should be a non-negative integer value.

Return

  • The LEFT function returns a string containing the specified number of leftmost characters from the input string.

3. Notes

  • If the str argument is NULL, the LEFT function will also return NULL.
  • If the length argument is negative, the LEFT function will return an empty string.
  • If the length argument is greater than the length of the input string, the LEFT function will return the entire input string.

4. Examples

Here are a few examples demonstrating the usage of the LEFT function in H2 database:

Example 1 - Extracting the leftmost 3 characters from a string:

SELECT LEFT('Hello World', 3) AS left_chars;

Output:

left_chars
----------
Hel

Example 2 - Extracting the leftmost characters from a column:

CREATE TABLE fruits (
id INT PRIMARY KEY,
name VARCHAR(10)
);

INSERT INTO fruits VALUES (1, 'Apple'), (2, 'Banana'), (3, 'Orange');

SELECT id, LEFT(name, 3) AS left_chars
FROM fruits;

Output:

id | left_chars
---+-----------
1 | App
2 | Ban
3 | Ora
  • right - Extract rightmost characters from a string.
  • substring - Extract a substring from a given string.