Mahmoud ElMansy: knowledge meant to be free

How to make fixed length digit

this is one of the easy posts for me i will talk about how to fixed length digit .
means i have column stored in the DB and i want to show hime with fiexd lenght like “00001 or 00123”

that is pretty much easy using one of the sql functions Called STUFF

this function as discriped in the MSDN as “The STUFF function inserts a string into another string. It deletes a specified length of characters in the first string at the start position and then inserts the second string into the first string at the start position.

writing the code
[cc lang=”sql”]

DECLARE @replaced VARCHAR(64)=’123′

SELECT STUFF(‘0000000000’, 11-LEN(@replaced), LEN(@replaced), @replaced);
— note that 11 here is the length of your fixed string +1