同名之前的前缀增量

时间:2016-07-28 13:26:31

标签: sql-server sql-server-2008 tsql

如果同名出现,如何自动将前缀从(00到99)递增?

ex:- Kiran,Kiran,Kiran
Then
ANS:= 01 Kiran 
      02 Kiran
      etc

note =没有空格

2 个答案:

答案 0 :(得分:1)

使用此处的split string functions之一:

declare @str varchar(max)
set @str='Kiran,Kiran,Kiran'

;with cte
as
(
select *, row_number() over (partition by item order by item)as rn
 from [dbo].[SplitStrings_Numbers](@str,',') 
 )
 select item,
 case when rn<10 then '0' +cast(rn as varchar(2)) else cast(rn  as varchar(2) ) end from cte

输出:

item    (No column name)
Kiran   01
Kiran   02
Kiran   03

答案 1 :(得分:1)

如果有帮助,我的Parser函数已经返回序列。例如:

Background="Red"

返回

Declare @String varchar(max)='Kiran,Kiran,Kiran'
Select * from [dbo].[udf-Str-Parse](@String,',')
  

现在,稍加扭曲,您可以格式化您想要的结果

Key_PS  Key_Value
1       Kiran
2       Kiran
3       Kiran

返回

Select Seq= RIGHT('00'+cast(Key_PS as varchar(10)),2),ParseVal=Key_Value from [dbo].[udf-Str-Parse](@String,',')

UDF

Seq ParseVal
01  Kiran
02  Kiran
03  Kiran