LEADING

LEADING is a SQL function used to remove leading characters, typically spaces, from a string. It is used in conjunction with the TRIM function to specify the part of the string that needs trimming.

LEADING(‘character’, ‘string’)

  • ‘character’: This parameter represents the leading characters that you want to remove from the ‘string’. It can be any valid character recognized by Oracle.
  • ‘string’: This parameter represents the original string from which you want to remove the leading characters specified in the ‘character’ parameter. It can be a string literal, variable, or column.

Example

SELECT LEADING('0' FROM '00012345') AS TrimmedString
FROM dual;

Output

TrimmedString
----------
12345

Explanation

The LEADING function in Oracle SQL is used to remove specified characters from the beginning of a string. In this example, it is used to remove ‘0’ characters from the starting of the string ‘00012345’. Thus, the output is ‘12345’.

LEADING(trim_character FROM string_expression)

  • trim_character: This refers to the set of characters to be removed from the leading end of string_expression. The trim_character can be a string of one or more characters. If para1 = ‘XYZ’ in ‘XYZFROMXYZ’, only ‘XYZ’ from the beginning of the string will be removed.
  • string_expression: This is the string from which the LEADING function will remove the specified characters. This string_expression is any valid string expression, including column names, string literals, or other string functions.

Example

SELECT
OriginCity,
LEADING('X' FROM OriginCity) AS NewCityName
FROM Orders;

Output

| OriginCity | NewCityName |
|------------|-------------|
| New York | XNew York |
| Houston | XHouston |
| Chicago | XChicago |

Explanation

In the above code snippet, the LEADING function adds a given character or characters at the beginning of the specified string. Here, the function appends ‘X’ to the start of each city’s name from the ‘Orders’ table. The output table represents the modified city names.

LEADING(character FROM text)

  • character: This parameter defines the set of characters that need to be removed from the leading end of the string. It can be a single character or multiple characters.
  • text: This parameter refers to the source string from which the characters will be removed from the leading end. This must be a string datatype. The function will not apply any changes if the string doesn’t start with the specified characters.

Example

SELECT LEADING(' PostgreSql ') AS trimmed_string;

Output

"PostgreSql "

Explanation

The LEADING function in PostgreSQL is used to remove leading specified characters from a string. In the given example, LEADING function is used to remove all leading spaces from the string ’ PostgreSql ’. As a result, the output is the same string, but without the spaces at the start.

For in-depth explanations and examples SQL keywords where you write your SQL, install our extension.