Wednesday, August 13, 2008

Lpad in SQL Server

By converting to a string and padding with the string..

Eg:

SELECT RIGHT(REPLICATE('0',6)+ CONVERT(VARCHAR(6),'7'),6)

SELECT RIGHT(REPLICATE('0',6)+ CONVERT(VARCHAR(6),SUBSTRING(RTRIM('T000006'),2,LEN(MAX('T000006'))-1)+1),6) FROM PS_PERSON