Pad Left - T SQL Query

I some cases, we will come across a situation where we think how easy will haven been our life, if they had included the PADLEFT() feature in SQL. Don't worry, we can achieve that functionality easily by a small query.

First Approach:


SELECT REPLICATE('0', 10-LEN(CAST(1234 AS VARCHAR))) + CAST(1234 AS VARCHAR)

The result will be: 0000001234

Second Approach: 

SELECT RIGHT('0000000000' + CAST(1234 AS VARCHAR), 10)

This will  also give the same result as above: 0000001234

Enjoy...

[Please feel free to post comments on this article. Or you can ask for more. I will be happy helping you]

No comments:

Post a Comment