Home > Database > Mysql Tutorial > How to Efficiently Split Comma-Separated Strings in SQL Server 2008 R2?

How to Efficiently Split Comma-Separated Strings in SQL Server 2008 R2?

Mary-Kate Olsen
Release: 2025-01-25 06:51:13
Original
544 people have browsed it

How to Efficiently Split Comma-Separated Strings in SQL Server 2008 R2?

In the SQL Server 2008 R2, high -efficiency segmentation string: complete solution

Question:

Extracting and separation values ​​from SQL Server 2008 R2 column segmentation string have always been a challenge for users. Although many solutions were proposed, none of them were successful in this specific version.

Solution:

In order to solve this problem, the following solutions provide a comprehensive function specifically for SQL Server 2008 R2:

To use this function, just use the following grammar queries to query the value:

<code class="language-sql">CREATE FUNCTION dbo.splitstring ( @stringToSplit VARCHAR(MAX) )
RETURNS
 @returnList TABLE ([Name] [nvarchar] (500))
AS
BEGIN

 DECLARE @name NVARCHAR(255)
 DECLARE @pos INT

 WHILE CHARINDEX(',', @stringToSplit) > 0
 BEGIN
  SELECT @pos  = CHARINDEX(',', @stringToSplit)  
  SELECT @name = SUBSTRING(@stringToSplit, 1, @pos-1)

  INSERT INTO @returnList 
  SELECT @name

  SELECT @stringToSplit = SUBSTRING(@stringToSplit, @pos+1, LEN(@stringToSplit)-@pos)
 END

 INSERT INTO @returnList
 SELECT @stringToSplit

 RETURN
END</code>
Copy after login

The above is the detailed content of How to Efficiently Split Comma-Separated Strings in SQL Server 2008 R2?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template