Home Database Mysql Tutorial SQL里面用自定义Split()完成个性化需求

SQL里面用自定义Split()完成个性化需求

Jun 07, 2016 pm 05:55 PM
split Custom function

为了满足需求自定义Split()在SQL中实现,代码很整洁,感兴趣的朋友可以参考下,或许对你学习sql语句有所帮助

代码如下:
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE function [dbo].[SplitString]
(
@Input nvarchar(max),
@Separator nvarchar(max)=',',
@RemoveEmptyEntries bit=1
)
returns @TABLE table
(
[Id] int identity(1,1),
[Value] nvarchar(max)
)
as
begin
declare @Index int, @Entry nvarchar(max)
set @Index = charindex(@Separator,@Input)
while (@Index>0)
begin
set @Entry=ltrim(rtrim(substring(@Input, 1, @Index-1)))
if (@RemoveEmptyEntries=0) or (@RemoveEmptyEntries=1 and @Entry'')
begin
insert into @TABLE([Value]) Values(@Entry)
end
set @Input = substring(@Input, @Index+datalength(@Separator)/2, len(@Input))
set @Index = charindex(@Separator, @Input)
end
set @Entry=ltrim(rtrim(@Input))
if (@RemoveEmptyEntries=0) or (@RemoveEmptyEntries=1 and @Entry'')
begin
insert into @TABLE([Value]) Values(@Entry)
end
return
end

函数、表都建好了,下面调用测试一下吧:
代码如下:
declare @str1 varchar(max), @str2 varchar(max), @str3 varchar(max)
set @str1 = '1,2,3'
set @str2 = '1###2###3'
set @str3 = '1###2###3###'
select [Value] from [dbo].[SplitString](@str1, ',', 1)
select [Value] from [dbo].[SplitString](@str2, '###', 1)
select [Value] from [dbo].[SplitString](@str3, '###', 0)

结果,截个图来看一下:
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to use the split method in Java String How to use the split method in Java String May 02, 2023 am 09:37 AM

The split method in String uses the split() method of String to split the String according to the incoming characters or strings and return the split array. 1. General usage When using general characters, such as @ or, as separators: Stringaddress="Shanghai@Shanghai City@Minhang District@Wuzhong Road";String[]splitAddr=address.split("@");System .out.println(splitAddr[0]+splitAddr[1]+splitAddr[2]+splitAddr[3

How to solve 'undefined: bytes.Split' error in golang? How to solve 'undefined: bytes.Split' error in golang? Jun 25, 2023 pm 02:02 PM

In the Go language, the bytes package is a package for manipulating byte types, and it contains many useful methods, such as the Split() method. However, when using the Split() method, you may encounter an "undefined: bytes.Split" error. This error is usually caused by incompatible Go versions or lack of necessary dependent libraries. This article will introduce some methods to solve this error. Method 1: Upgrade the Go version as follows

How to write custom functions in MySQL using Python How to write custom functions in MySQL using Python Sep 22, 2023 am 08:00 AM

How to use Python to write custom functions in MySQL MySQL is an open source relational database management system that is often used to store and manage large amounts of data. As a powerful programming language, Python can be seamlessly integrated with MySQL. In MySQL, we often need to use custom functions to complete some specific calculations or data processing operations. This article will introduce how to use Python to write custom functions and integrate them into MySQL. For writing custom functions,

What is the use of split method in go language What is the use of split method in go language Jan 28, 2023 pm 01:37 PM

In the Go language, the Split() method is used to split a string. You can use delimiters to divide the string into a list of substrings, and the substrings are returned in the form of slices. Split() is a method of the strings package. You need to import the strings package before using it. The syntax is "strings.Split (string to be split, delimiter)".

How to use split in python How to use split in python Nov 17, 2023 am 10:13 AM

In Python, split() is a commonly used string method that splits a string into substrings and returns a list containing these substrings. This method can split a string into multiple parts based on the specified delimiter. The basic syntax is "str.split(separator, maxsplit)", str is the string to be split, separator is the separator, and maxsplit is an optional parameter, indicating the maximum number of splits.

In-depth analysis of the declaration and call of JS custom functions In-depth analysis of the declaration and call of JS custom functions Aug 03, 2022 pm 07:28 PM

A function is a set of reusable code blocks that perform a specific task (have a specific functionality). In addition to using built-in functions, we can also create our own functions (custom functions) and then call this function where needed. This not only avoids writing repeated code, but also facilitates the later maintenance of the code.

Detailed explanation of split command in Linux Detailed explanation of split command in Linux Feb 21, 2024 pm 06:06 PM

Detailed explanation of split command in Linux split is a commonly used command in Linux. It is used to split a file into multiple smaller files. In this article, we will introduce the usage of split command in detail and provide some specific code examples. 1. Command syntax The basic syntax of the split command is as follows: split [options] [input file] [output file prefix] options: -: Split the file according to the specified number of lines, the default is 1000 lines. -

Linux large file partition: Experienced users teach you the split command to easily handle it Linux large file partition: Experienced users teach you the split command to easily handle it Feb 26, 2024 am 08:10 AM

I am honored to meet you. I am a professional who has been in the Linux field for more than ten years and is proficient in various techniques and tools. This is how you should learn Linux tonight, and I would like to share with you my experience on how to effectively partition large files in the Linux environment. 1. Fearless in the face of huge files Linux large file splitting As a senior Linux user, I am good at handling all types of files, regardless of their size. Sometimes it may be a little troublesome to split, transfer or back up large files in Linux, but this does not pose much of a challenge to Linux systems. 2. Use the split command to easily split files. Under Linux systems, the split command is very practical. A simple tap to issue a command can easily

See all articles