Tuesday, 20 February 2024

LPAD in MSSQL

In Microsoft SQL Server, you can use the REPLICATE and LEN functions along with string concatenation to achieve the equivalent of LPAD. Here's an example:


SELECT 

    RIGHT(REPLICATE('*', 15) + employee_name, 15) AS padded_name

FROM 

    employees;


In this query:


- REPLICATE('*', 15) generates a string of asterisks (*) repeated 15 times.

- employee_name is concatenated to the generated string.

- RIGHT(..., 15) extracts the rightmost 15 characters of the concatenated string, effectively padding the employee_name to the left with asterisks (*) to reach a total length of 15 characters.


This query will produce a result where each employee_name is padded with asterisks (*) to a length of 15 characters from the left. Adjust the 15 in the REPLICATE and RIGHT functions as needed for your specific padding length.

No comments:

Post a Comment