SQL Server implements the split function to split strings and Its Usage example. sqlsplit

Source: Internet
Author: User
Tags how to use sql server how to use sql

SQL Server implements the split function to split strings and Its Usage example. sqlsplit

This article describes how to use SQL Server to split strings using the split function. We will share this with you for your reference. The details are as follows:

/* Function name: f_SplitToNvarchar function: function update record for split function: design idea: a string that combines nvarchar characters, separate them into a table with only one nvarchar type */create function [dbo]. [f_SplitToNvarchar] (@ SourceSql NVARCHAR (MAX), -- source separator string @ StrSeprate VARCHAR (10) -- separator) RETURNS @ temp TABLE (col NVARCHAR (MAX )) ASBEGINDECLARE @ I INTSET @ SourceSql = RTRIM (LTRIM (@ SourceSql) SET @ I = CHARINDEX (@ StrSeprate, @ SourceSql) WHILE @ I> = 1 begin insert @ temp VALUES (LEFT (@ SourceSql, @ I-1) SET @ SourceSql = SUBSTRING (@ SourceSql, @ I + 1, LEN (@ SourceSql)-@ I) SET @ I = CHARINDEX (@ StrSeprate, @ SourceSql) ENDIF @ SourceSql <> '\' INSERT @ temp VALUES (@ SourceSql) RETURNENDGO

Call example:

SELECT col FROM f_SplitToNvarchar ('1, 2, 3, 4 ',',');

:

I hope this article will help you design SQL Server database programs.

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.