sql语句格式化数字(前面补0)
Posted Li Essay
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了sql语句格式化数字(前面补0)相关的知识,希望对你有一定的参考价值。
将一个数字例如33,或1使用t-sql语句转换成033或001 以下是详细分析: 1、select power(10,3)得到1000 2、select cast(1000+33 as varchar) 将1000转换类型 3、select 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
以上是关于sql语句格式化数字(前面补0)的主要内容,如果未能解决你的问题,请参考以下文章