Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- CREATE FUNCTION [dbo].[fn_SplitList]
- (
- @RowData varchar(8000),
- @SplitOn varchar(5)
- )
- RETURNS @RtnValue table
- (
- Id int identity(1,1),
- Data varchar(100)
- )
- AS
- BEGIN
- Declare @Cnt int
- Set @Cnt = 1
- While (Charindex(@SplitOn,@RowData)>0)
- Begin
- Insert Into @RtnValue (data)
- Select
- Data = ltrim(rtrim(Substring(@RowData,1,Charindex(@SplitOn,@RowData)-1)))
- Set @RowData = Substring(@RowData,Charindex(@SplitOn,@RowData)+1,len(@RowData))
- Set @Cnt = @Cnt + 1
- End
- Insert Into @RtnValue (data)
- Select Data = ltrim(rtrim(@RowData))
- Return
- END
- declare @table1 table(id int primary key
- ,words varchar(max))
- declare @id int
- declare @words varchar(max)
- insert into @table1 values(0, 'word1, word2, , word3, word4')
- insert into @table1 values(1, 'word1, word2, word3, ,')
- insert into @table1 values(2, 'word1,,,, ; word2')
- insert into @table1 values(3, ';word1 word2, word3')
- declare updateCursor cursor for
- select id
- ,words
- from @table1
- open updateCursor
- fetch next from updateCursor into @id, @words
- while @@fetch_status = 0
- begin
- declare @row varchar(255)
- select @row = coalesce(@row+', ', '') + data
- from dbo.fn_SplitList(@words, ',')
- order by id desc
- update @table1
- set words = @row
- where id = @id
- fetch next from updateCursor into @id, @words
- end
- close updateCursor
- deallocate updateCursor
- select *
- from @table1
- update t_desc set name =
- (select name
- from
- (select name,
- case when wb is null then rname when wb >= we then stuff(rname, wb, 128, REVERSE (substring(rname, wb, 128))) else
- stuff(rname, wb, we-wb+1, REVERSE(substring(rname, wb, we-wb+1))) end rname,
- case when wb is null or wb > we then null else PATINDEX('%[a-z0-9]%', SUBSTRING(rname, we+1, 128))+we end wb,
- case when we is null or wb > we then null else PATINDEX('%[a-z0-9][^a-z0-9]%', SUBSTRING(rname, we+1, 128))+we end we
- from
- (select name,
- case when wb is null then rname when wb >= we then stuff(rname, wb, 128, REVERSE (substring(rname, wb, 128))) else
- stuff(rname, wb, we-wb+1, REVERSE(substring(rname, wb, we-wb+1))) end rname,
- case when wb is null or wb > we then null else PATINDEX('%[a-z0-9]%', SUBSTRING (rname, we+1, 128))+we end wb,
- case when we is null or wb > we then null else PATINDEX('%[a-z0-9][^a-z0-9]%', SUBSTRING(rname, we+1, 128))+we end we
- from
- (select name,
- case when wb is null then rname when wb >= we then stuff(rname, wb, 128, REVERSE (substring(rname, wb, 128))) else
- stuff(rname, wb, we-wb+1, REVERSE(substring(rname, wb, we-wb+1))) end rname,
- case when wb is null or wb > we then null else PATINDEX('%[a-z0-9]%', SUBSTRING (rname, we+1, 128))+we end wb,
- case when we is null or wb > we then null else PATINDEX('%[a-z0-9][^a-z0-9]%', SUBSTRING(rname, we+1, 128))+we end we
- from
- (select name,
- stuff(rname, wb, we-wb+1, REVERSE(substring(rname, wb, we-wb+1))) rname,
- PATINDEX('%[a-z0-9]%', SUBSTRING(rname, we+1, 128))+we wb,
- PATINDEX('%[a-z0-9][^a-z0-9]%', SUBSTRING(rname, we+1, 128))+we we
- from
- (select name, rname,
- PATINDEX('%[a-z0-9]%', rname) wb,
- PATINDEX('%[a-z0-9][^a-z0-9]%', rname) we
- from
- (select t_desc.name, REVERSE(name) rname) t1) t2) t3) t4) t5) t6)
Add Comment
Please, Sign In to add comment