Home > Database > Mysql Tutorial > SQL Server字符串切割函数

SQL Server字符串切割函数

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-06-07 18:07:55
Original
1700 people have browsed it

创建用户定义函数,它是返回值的已保存的 Transact-SQL 例程。用户定义函数不能用于执行一组修改全局数据库状态的操作。与系统函数一样,用户定义函数可以从查询中唤醒调用。也可以像存储过程一样,通过 EXECUTE 语句执行

代码如下:
CREATE FUNCTION fGetStrBySplit
(
@Source VARCHAR(max),
@Index INT,
@SplitChar VARCHAR(1)
)
RETURNS varchar(MAX)
AS
BEGIN

DECLARE @Len INT
DECLARE @n INT = 0
DECLARE @ChIndex INT
DECLARE @Result VARCHAR(MAX)
--获取总长度
SET @Len = LEN(@Source)
--获取切割字符位置
SET @ChIndex = CHARINDEX(@SplitChar,@Source)

WHILE @ChIndex > 0
BEGIN
IF(@n = @Index)
BEGIN
SET @Source = SUBSTRING(@Source,0,@ChIndex)
BREAK
END

SET @Source = SUBSTRING(@Source,@ChIndex+1,@Len)
SET @ChIndex = CHARINDEX(@SplitChar,@Source)

SET @Len = LEN(@Source)
SET @n = @n + 1

END

RETURN @Source
END
GO

--调用
DECLARE @value VARCHAR(max)
SET @value = dbo.fGetStrBySplit('645002*01_45854_183677_12',0,'_')
PRINT @value

结果:
645002*01
--1

45854

--2

183677
Related labels:
sql
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 Issues
sql file
From 1970-01-01 08:00:00
0
0
0
php - Overhead of prepare vs sql?
From 1970-01-01 08:00:00
0
0
0
Print sql statement
From 1970-01-01 08:00:00
0
0
0
Pass array to SQL insert query using PHP
From 1970-01-01 08:00:00
0
0
0
sql optimization or
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template