使用 SQL 函数用空格填充字符串 [英] Using a SQL function to pad strings with spaces

查看:111
本文介绍了使用 SQL 函数用空格填充字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用用户定义的函数填充空格是否效率低下?我有一个填充函数,比使用内置的 REPLICATE 函数更直观,但我担心它会导致代码效率低下.

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.

填充必须在 SQL 中完成.

The padding must be done in SQL.

推荐答案

您可以根据 paddibg 方向使用 RIGHT 或 LEFT.
例如:

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

SELECT RIGHT('11111' + originalString, 5) 

这将用 1 填充左边的字符串以形成 5 个字母的字符串.(我使用 1s 而不是空格,所以它很容易阅读.对于空格,您可以使用 SPACE 函数:

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) 

或者按照 Gordon Linoff 在评论中的建议简单地转换为字符

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

这篇关于使用 SQL 函数用空格填充字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆