Sql server 2008 选择查询长度问题

Sql server 2008 选择查询长度问题,sql-server-2008,Sql Server 2008,可能重复: 我有一个customerID长度为10的customer表 但很少有客户的长度是3或5 ex: 3445 34 789 7800 但是输出应该如下所示,如果长度小于10,我需要在这里加前缀零 0000003445 0000000034 0000000789 0000007800 获取此数据的脚本是: select customerID from customer 试试这个 SELECT RIGHT('0000000000'+`customerID`, 10) FROM `c

可能重复:

我有一个customerID长度为10的customer表 但很少有客户的长度是3或5

ex: 
3445
34
789
7800
但是输出应该如下所示,如果长度小于10,我需要在这里加前缀零

0000003445
0000000034
0000000789
0000007800
获取此数据的脚本是:

select customerID from customer
试试这个

SELECT RIGHT('0000000000'+`customerID`, 10) FROM `customer`
试试这个

SELECT RIGHT('0000000000'+`customerID`, 10) FROM `customer`

你可以用这种方式

SELECT RIGHT('0000000000' + RTRIM('3445'), 10)
就你而言

SELECT RIGHT('0000000000' + RTRIM(customerID), 10) AS New_CustomerID
FROM Customer

你可以用这种方式

SELECT RIGHT('0000000000' + RTRIM('3445'), 10)
就你而言

SELECT RIGHT('0000000000' + RTRIM(customerID), 10) AS New_CustomerID
FROM Customer

请尝试以下代码:

因为您使用的是sql server 2008,所以可以使用replicate函数向值中添加零

Select ltrim(right(replicate(0,10) + <column>,10)) 
from your_table
选择ltrim(右(复制(0,10)+,10))
从你的桌子上

请尝试以下代码:

因为您使用的是sql server 2008,所以可以使用replicate函数向值中添加零

Select ltrim(right(replicate(0,10) + <column>,10)) 
from your_table
选择ltrim(右(复制(0,10)+,10))
从你的桌子上

这将避免计算零的必要性,但速度会较慢。@NoobASThreeDeveloper:由于复制而导致的速度可以忽略不计...)这将避免计算零的必要性,但速度会较慢。@NoobASThreeDeveloper:由于复制而导致的速度可以忽略不计…..)