T-SQL 将整数格式化为 2 位字符串 [英] T-SQL Format integer to 2-digit string



我找不到在 T-SQL 中执行此操作的简单方法.

I can't find a simple way to do this in T-SQL.

例如,我有一个列 (SortExport_CSV),它返回一个整数2"到 90.如果存储的数字是单个数字,我需要将其转换为以 0 开头的 2 位字符串.我曾尝试使用 CAST,但我不知道如何以首选格式 (0#) 显示样式

I have for example a column (SortExport_CSV) that returns an integer '2' thru 90. If the stored number is a single digit, I need it to convert to a 2 digit string that begins with a 0. I have tried to use CAST but I get stuck on how to display the style in the preferred format (0#)

当然,在前端(SSRS、MSAccess、Excel 等)上执行此操作很容易,但在这种情况下,我没有前端,必须为原始数据集提供已格式化的 2 位字符串.

Of course it is easy to do this on the front end (SSRS, MSAccess, Excel, etc) but in this instance I have no front end and must supply the raw dataset with the already formatted 2 digit string.


select right ('00'+ltrim(str( <number> )),2 )
登录 关闭
发送“验证码”获取 | 15天全站免登陆