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

Tuesday, 20 February 2024

LPAD in DB2

In IBM DB2, you can use the LPAD function to pad a string to the left with a specific set of characters until it reaches a specified length. Here's how you can use LPAD with an example:


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

FROM employees;


In this query:


- employee_name is the original string to be padded.

- 15 is the total length of the resulting string after padding.

- '*' is the character to pad the original string with.


This query will retrieve the employee_name column from the employees table and pad each name with asterisks (*) to make them 15 characters long from the left. The result will be aliased 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. Adjust the length and padding character as needed for your specific requirements.

No comments:

Post a Comment

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