将一个数字例如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
时间: 2024-10-07 13:18:21