Cade Roux Cade Roux - 1 year ago 68
SQL Question

Most efficient T-SQL way to pad a varchar on the left to a certain length?

As compared to say:

REPLICATE(@padchar, @len - LEN(@str)) + @str

Answer Source

This is simply an inefficient use of SQL, no matter how you do it.

perhaps something like

right('XXXXXXXXXXXX'+ rtrim(@str), @n)

where X is your padding character and @n is the number of characters in the resulting string (assuming you need the padding because you are dealing with a fixed length).

But as I said you should really avoid doing this in your database.