Java教程

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

本文主要是介绍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

这篇关于sql语句格式化数字(前面补0)的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!