Guest User

Untitled

a guest
Jun 23rd, 2018
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.17 KB | None | 0 0
  1. CREATE FUNCTION [dbo].[fn_SplitList]
  2. (
  3. @RowData varchar(8000),
  4. @SplitOn varchar(5)
  5. )
  6. RETURNS @RtnValue table
  7. (
  8. Id int identity(1,1),
  9. Data varchar(100)
  10. )
  11. AS
  12. BEGIN
  13. Declare @Cnt int
  14. Set @Cnt = 1
  15.  
  16. While (Charindex(@SplitOn,@RowData)>0)
  17. Begin
  18. Insert Into @RtnValue (data)
  19. Select
  20. Data = ltrim(rtrim(Substring(@RowData,1,Charindex(@SplitOn,@RowData)-1)))
  21.  
  22. Set @RowData = Substring(@RowData,Charindex(@SplitOn,@RowData)+1,len(@RowData))
  23. Set @Cnt = @Cnt + 1
  24. End
  25.  
  26. Insert Into @RtnValue (data)
  27. Select Data = ltrim(rtrim(@RowData))
  28.  
  29. Return
  30. END
  31.  
  32. declare @table1 table(id int primary key
  33. ,words varchar(max))
  34. declare @id int
  35. declare @words varchar(max)
  36.  
  37. insert into @table1 values(0, 'word1, word2, , word3, word4')
  38. insert into @table1 values(1, 'word1, word2, word3, ,')
  39. insert into @table1 values(2, 'word1,,,, ; word2')
  40. insert into @table1 values(3, ';word1 word2, word3')
  41.  
  42. declare updateCursor cursor for
  43. select id
  44. ,words
  45. from @table1
  46.  
  47. open updateCursor
  48. fetch next from updateCursor into @id, @words
  49. while @@fetch_status = 0
  50. begin
  51. declare @row varchar(255)
  52.  
  53. select @row = coalesce(@row+', ', '') + data
  54. from dbo.fn_SplitList(@words, ',')
  55. order by id desc
  56.  
  57. update @table1
  58. set words = @row
  59. where id = @id
  60.  
  61. fetch next from updateCursor into @id, @words
  62. end
  63. close updateCursor
  64. deallocate updateCursor
  65.  
  66. select *
  67. from @table1
  68.  
  69. update t_desc set name =
  70. (select name
  71. from
  72. (select name,
  73. case when wb is null then rname when wb >= we then stuff(rname, wb, 128, REVERSE (substring(rname, wb, 128))) else
  74. stuff(rname, wb, we-wb+1, REVERSE(substring(rname, wb, we-wb+1))) end rname,
  75. case when wb is null or wb > we then null else PATINDEX('%[a-z0-9]%', SUBSTRING(rname, we+1, 128))+we end wb,
  76. 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
  77. from
  78. (select name,
  79. case when wb is null then rname when wb >= we then stuff(rname, wb, 128, REVERSE (substring(rname, wb, 128))) else
  80. stuff(rname, wb, we-wb+1, REVERSE(substring(rname, wb, we-wb+1))) end rname,
  81. case when wb is null or wb > we then null else PATINDEX('%[a-z0-9]%', SUBSTRING (rname, we+1, 128))+we end wb,
  82. 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
  83. from
  84. (select name,
  85. case when wb is null then rname when wb >= we then stuff(rname, wb, 128, REVERSE (substring(rname, wb, 128))) else
  86. stuff(rname, wb, we-wb+1, REVERSE(substring(rname, wb, we-wb+1))) end rname,
  87. case when wb is null or wb > we then null else PATINDEX('%[a-z0-9]%', SUBSTRING (rname, we+1, 128))+we end wb,
  88. 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
  89. from
  90. (select name,
  91. stuff(rname, wb, we-wb+1, REVERSE(substring(rname, wb, we-wb+1))) rname,
  92. PATINDEX('%[a-z0-9]%', SUBSTRING(rname, we+1, 128))+we wb,
  93. PATINDEX('%[a-z0-9][^a-z0-9]%', SUBSTRING(rname, we+1, 128))+we we
  94. from
  95. (select name, rname,
  96. PATINDEX('%[a-z0-9]%', rname) wb,
  97. PATINDEX('%[a-z0-9][^a-z0-9]%', rname) we
  98. from
  99. (select t_desc.name, REVERSE(name) rname) t1) t2) t3) t4) t5) t6)
Add Comment
Please, Sign In to add comment