Try this,
drop table #t
drop table #t1
create table #t (c varchar(1000))
insert into #t values ('DDXXD4444DDD')
create table #t1 (c char(1),val varchar(20))
insert into #t1 values ('D','SXM')
insert into #t1 values ('X','DSN')
insert into #t1 values ('4','MXD')
;WITH CTE AS (
SELECT Number,SUBSTRING(c, 1 + Number, 1) [char]
FROM master..spt_values,#t
WHERE Number < DATALENGTH(c)
AND type = 'P'
),CTE_REPLACE AS (
SELECT Stuff((SELECT ' ' + VAL
FROM (SELECT [NUMBER],VAL FROM CTE A INNER JOIN #T1 B ON A.[CHAR]=B.C) X
FOR xml path('')), 1, 1, '') VAL
)
SELECT Replace(Val,' ','') as ReplaceColumn FROM CTE_REPLACE
Regards, RSingh
No comments:
Post a Comment