带有前导字符的SQL格式

时间:2017-03-17 22:34:36

标签: sql sql-server format

我希望customerId被格式化为“TUH000000”我的SQL已经吐出1,2,3等的int。所以如果客户ID是1,我希望它打印TUH000001等等。

这是我的代码,以便在表格中输出我需要的所有内容,减去格式。有什么想法吗?

SELECT r.CustomerID,r.Year AS [Year],r.UnpaidBalance AS [UnpaidBalance],ci.FName AS [FName], ci.LBName AS [LBName],ci.IdType AS [IdType],ci.Id AS [Id],ci.OwnerId AS OwnerId, a.Line1 AS [Address1], a.Line2 AS [Address2], a.City AS [City], a.StateId AS [State], a.Zip AS [Zip]
FROM #ThisReport r
LEFT JOIN #CustomerInfo ci ON r.CustomerID = ci.CustomerId
LEFT JOIN Address a ON ci.mAddID = a.AddressId
ORDER BY CAST(r.CustomerID as int) ASC;

1 个答案:

答案 0 :(得分:0)

您的代码看起来像SQL Server。一种方法是:

select prefix + right(replicate('0', 6) + cast(x as varchar(255)), 6)

我不确定要合并哪些列。 Pehaps:

select 'TUH' + right(replicate('0', 6) + cast(CustomerId as varchar(255)), 6)