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
来源;https://www.cnblogs.com/liessay/p/8058928.html
标签:语句,格式化,val,UNION,select,sql,35040102,35040105,SELECT 来源: https://www.cnblogs.com/zhou0811/p/16576138.html