sql语句格式化数字(前面补0)

将一个数字例如33,或1使用t-sql语句转换成033或001
以下是详细分析:
1select power(10,3)得到1000
 2select cast(1000+33 as varchar) 将1000转换类型
3select right(100033,3) 从右边取3个字符得到033
将1格式化同上
select right(cast(power(10,3) as varchar)+33,3)
--用法例子
CREATE
TABLE #T ( val NVARCHAR(50) NOT NULL ) INSERT INTO #t SELECT '35040112' UNION ALL SELECT '35040112' UNION ALL SELECT '35040112' UNION ALL SELECT '35040102' UNION ALL SELECT '35040102' UNION ALL SELECT '35040102' UNION ALL SELECT '35040102' UNION ALL SELECT '35040105' UNION ALL SELECT '35040105' UNION ALL SELECT '35040105' UNION ALL SELECT '35040105' UNION ALL SELECT '35040105' UNION ALL SELECT '35040105' UNION ALL SELECT '35040105' UNION ALL SELECT '35040105' UNION ALL SELECT '35040105' UNION ALL SELECT '35040105' UNION ALL SELECT '35040105' SELECT val AS 查出的料号,val +'-'+ right(cast(power(10,2) as varchar) +CAST(ROW_NUMBER() OVER (PARTITION BY val ORDER BY val) AS NVARCHAR),2) AS 需变成的这种 FROM #T

posted @ 2017-12-18 18:03  liessay  阅读(7958)  评论(0编辑  收藏  举报