Using a SQL function to pad strings with spaces

前端 未结 1 396
孤城傲影
孤城傲影 2020-12-21 20:05

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 afrai

相关标签:
1条回答
  • 2020-12-21 20:15

    You can use RIGHT or LEFT depending on the paddibg 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

    0 讨论(0)
提交回复
热议问题