Procedure for Split the words in SQL Sever

Here it is the procedure to Split the words using comma seperator. still you can use different character for split instead of comma(','). Here i m using 'E,l,e,p,h,a,n,t' as word with comma characters.
so the output should be 'E','l','e','p','h','a','n','t'.
  1. Declare @name as varchar(20)
  2. Declare @i as int
  3. Declare @char as char
  4. Declare @word as varchar(20)
  5. select @name='E,l,e,p,h,a,n,t'
  6. set @word=''
  7. set @i=1
  8. while @i<=len(@name) begin set @char=substring(@name,@i,1) if @char<> ','
  9. begin
  10. set @word=@word+@char
  11. end
  12. else if (@char=',' and @i<>len(@name))
  13. begin
  14. print @word
  15. set @word=''
  16. end
  17. ---Print the last word
  18. if @i=len(@name)
  19. begin
  20. print @word
  21. end
  22. set @i=@i+1
  23. end



















Output of this query would be,

Comments

Popular posts from this blog

SQL SERVER – TRIM() Function – UDF TRIM()

Delete duplicate records from a Table in SQL Server