T-SQL- Pad left and Pad right functions in SQL
Today in this article, we will see how to add Pad left and Pad right functions in SQL with examples.
Learn how to add Pad left and Pad right functions in SQL with examples, including adding leading zeros to SQL fields.
We have below EmployeeID which is defined as a string type and length of 4 characters.
Example
1 123 1234
SQL- Pad right logic to add trailing zeros to SQL Field
The expected output is as below,
1000 1200 1230 1234
SQL Query pattern
SELECT LEFT (<SQL FIELD> +'0000', 4) FROM <Table Name>;
Example
SELECT LEFT ([EmployeeID]+'0000', 4) FROM Table_2;
SQL- Pad left zeros i.e to add leading zeros to SQL Field
The expected output is as below,
0001 0012 0123 1234
SQL Query pattern
SELECT RIGHT('0000' + <SQL FIELD>, 4) FROM <Table Name>
Example
SELECT RIGHT ('0000'+ [EmployeeID], 4) FROM Table_2;
That’s all! Happy coding!
Does this help you fix your issue?
Do you have any better solutions or suggestions? Please sound off your comments below.
Please bookmark this page and share it with your friends. Please Subscribe to the blog to receive notifications on freshly published(2024) best practices and guidelines for software design and development.