2

Is it inefficient to use a user defined function to pad spaces? I have a padding function that I'd more intuitive than using the built in REPLICATE function but I am afraid it is introducing inefficiency into the code.

The padding must be done in SQL.

Gordon Linoff
  • 1,242,037
  • 58
  • 646
  • 786
a b
  • 57
  • 2
  • 8

1 Answers1

3

You can use RIGHT or LEFT depending on the padding direction.
For example:

SELECT RIGHT('11111' + originalString, 5) 

This will pad your string with on the left with 1s to make a 5 letters string. (I've used 1s instead of spaces so it will be easy to read. For spaces you can use the SPACE function:

SELECT RIGHT(SPACE(5) + OriginlString, 5)

to pad to the right you can do this:

SELECT LEFT(OriginalString + SPACE(5), 5) 

or simply convert to char as suggested by Gordon Linoff in the comments

Manngo
  • 14,066
  • 10
  • 88
  • 110
Zohar Peled
  • 79,642
  • 10
  • 69
  • 121
  • Thanks. What we have is a lot of calls to user defined functions for right and left padding already written to do this and I'm wondering if it is worth replacing it with the suggestions above. – a b Apr 25 '15 at 15:47
  • 1
    You shold know better then anyone since you know you system best. It's probably possible to replace one udf call ant test performance, and then decide what to do. – Zohar Peled Apr 25 '15 at 16:24