SQL Server Split String functions

Source: Internet
Author: User

--Description: Splitting a string function--SELECT * FROM dbo. Split (' a,b,c,d,e,f,g ', ', ')-- =============================================CREATE FUNCTION [dbo].[Split](@Text VARCHAR(8000) ,@Sign NVARCHAR(4000) )    RETURNS @tempTable TABLE(IDINT IDENTITY(1,1)PRIMARY KEY,[TempVal] VARCHAR(4000) )      as BEGIN DECLARE @StartIndex INT --where to start looking DECLARE @FindIndex INT --location found DECLARE @Content VARCHAR(4000)--the value foundSET @StartIndex = 1 --The find position of a string in T-SQL starts at 1SET @FindIndex = 0 --start loop lookup string comma  while(@StartIndex <= LEN(@Text))BEGIN --The return value is where the string was foundSELECT @FindIndex = CHARINDEX(@Sign,@Text,@StartIndex)--Judging if there's no found. return 0 IF(@FindIndex = 0     OR @FindIndex  is NULL)BEGIN --if we don't find them , we're done.SET @FindIndex = LEN(@Text)+ 1 END --Initialize the location of the next lookupSET @Content = LTRIM(RTRIM(SUBSTRING(@Text,@StartIndex,@FindIndex - @StartIndex)))SET @StartIndex = @FindIndex + 1 --Insert the value you are looking for in the table type that you want to returnINSERT  into @tempTable([TempVal])VALUES(@Content)END RETURN END

SQL Server Split String functions

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.