SQL split string, SQL split

Source: Internet
Author: User

SQL split string, SQL split

Create function [dbo]. [fn_Split]
(
@ SourceSql nvarchar (max), -- source separator string
@ StrSeprate varchar (10) -- delimiter
)
Returns @ temp table (a nvarchar (max ))
As
Begin
Declare @ I int
Set @ 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)
End
If @ SourceSql <> '\'
Insert @ temp values (@ SourceSql)
Return
End
Go


SQL split string

See what database you are.
SQL Server does not support regular-expression-like splitting. Only string functions are supported.
LEFT (name, CHARINDEX ('.', name, 0)
The middle part can be implemented using SUBSTRING instead of LEFT.

SQL string splitting ??

OK! The test is passed. Try it. Add more points if you feel better.

Exec decord_a '1a @ b2 @ c3 @ e4 @ f5'

--- This stored procedure can extract strings containing any @ character. The query analyzer or front-end dataset can be used to obtain results through temporary table output.

Create procedure decord_A
@ Str varchar (100)
AS
Declare @ n int, @ SN INT
SET @ N = LEN (@ STR)-LEN (REPLACE (@ STR, '@', '') -- extract the number of characters @
SET @ SN = 1
CREATE table # TEMP (sn int, cstr varchar (100 ))
WHILE @ SN <= @ N
BEGIN
Insert into # temp select @ SN, LEFT (@ STR, CHARINDEX ('@', @ STR)-1)
SELECT @ SN = @ SN + 1, @ STR = SUBSTRING (@ STR, CHARINDEX ('@', @ STR) + 1,100)
END
Insert into # temp select @ N + 1, @ STR
SELECT * FROM # TEMP order by sn
GO

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.