sql server中将一列多行的数据转成一列一行,和一行数据解析成一行多列

create function dbo.split(@CID nvarchar(2000), @delimiter nchar)
returns @t table (a nvarchar(20))
as
begin
declare @idx as int

while len(@CID) > 0
begin
    set @idx = charindex(@delimiter, @CID)
    if @idx = 0 and len(@CID) > 0
    begin
        insert into @t values(@CID)
        break
    end
    if @idx > 1
    begin
        insert into @t values(left(@CID, @idx - 1))
        set @CID = right(@CID, len(@CID) - @idx)
    end else
        set @CID = right(@CID, len(@CID) - @idx)
end
return
end
go

--select charindex('-','15411-111111111111111111-11-454-ewr')
--select left('15411-111111111111111111-11-454-ewr', charindex('-','15411-111111111111111111-11-454-ewr') - 1)
--select LEN('15411-111111111111111111-11-454-ewr')
--select right('15411-111111111111111111-11-454-ewr',LEN('15411-111111111111111111-11-454-ewr')-charindex('-','15411-111111111111111111-11-454-ewr'))
select * from dbo.split('111111111111-2222222222222-33333333333-asdffasd', '-')
drop function dbo.split

/*****************下面是将某一列的数据填到一行显示,用','分割**********/


declare @ProcessTrackingTableName varchar(4000)
set @ProcessTrackingTableName=''
select @ProcessTrackingTableName =@ProcessTrackingTableName+ ProcessTrackName+',' from tb_RenWuFenPei where CONVERT(varchar(12) ,ExecuteTime,111)=CONVERT(varchar(12),'2012/03/28', 111)
and ObjectDepartments='监督一科' and DistributionType='101000' and ObjectGroup='一组'
select @ProcessTrackingTableName

©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页