Welcome to plsql4all.blogspot.com SQL, MYSQL, ORACLE, TERADATA, MONGODB, MARIADB, GREENPLUM, DB2, POSTGRESQL.

Tuesday, 20 February 2024

LPAD in Oracle

In Oracle, the LPAD function is used to pad a string to the left with a specific set of characters until it reaches a specified length. Here's the syntax for the LPAD function:


LPAD(string, length, [pad_string])


- string: The original string to be padded.

- length: The total length of the resulting string after padding.

- pad_string (optional): The character(s) to pad the original string with. If omitted, Oracle pads with spaces by default.


Here's an example of how to use the LPAD function in Oracle:


Suppose you have a table employees with columns employee_id and employee_name, and you want to retrieve the employee names padded to a length of 15 characters with asterisks (*) to the left:


SELECT LPAD(employee_name, 15, '*') AS padded_name

FROM employees;


This query will retrieve the employee_name column from the employees table, pad each name with asterisks (*) to make them 15 characters long from the left, and alias the result as padded_name.


For example, if you have an employee named "John", the result would be "***********John" because "John" is only 4 characters long, and it's padded with asterisks to reach a length of 15 characters from the left.

No comments:

Post a Comment

Please provide your feedback in the comments section above. Please don't forget to follow.