A SQL Function which used to split string by char

本文介绍了一个用于SQL的用户定义函数(fnSplitByDelim),该函数可以将包含分隔符的字符串拆分为多个部分,并返回一个表格形式的结果集。此函数适用于需要处理字符串并将其按特定字符分割的场景。
IF exists (SELECT * from dbo.sysobjects where id = object_id(N'[dbo].[fnSplitByDelim]'and xtype in (N'FN', N'IF', N'TF'))
drop function [dbo].[fnSplitByDelim]
GO

/****** Object:  UserDefinedFunction [dbo].[fnSplitByDelim]    Script Date: 05/25/2006 20:22:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

CREATE FUNCTION [dbo].[fnSplitByDelim]
(
    
@SourceString VARCHAR(max),
    
@Delim CHAR(1)
)
RETURNS @Result TABLE(SplittedValue varchar(255), Ident INT IDENTITY not null)
BEGIN

-- ============================================================================================ --
--
                                      FinTool                                                    --
--
 ============================================================================================ --

/***********************************************************************************
** Desc:    Function used to return the values from string with seperator
**            
**            
**            
**
**
** Note:    
**                
**
** Called By:    Stored Procedures
** 
**
** Author:   v-svarad
** Date:     Oct 1, 2006
************************************************************************************
** Change History
************************************************************************************
** Date         Author     Description
** ----------   ---------  ----------------------------------------------------------
** 10-01-2006    v-svarad   Created

**********************************************************************************
*/


    
DECLARE @Wordstart int@WordEND int@DoubleDelim varchar(2)
    
    
SET @Wordstart = 1
    
SET @WordEND = 1
    
SET @DoubleDelim = REPLICATE(@Delim2)

    
-- Prepare the data
    WHILE CHARINDEX(@DoubleDelim@SourceString> 0
        
SET @SourceString = REPLACE(@SourceString@DoubleDelim@Delim)
    
-- Left-trim it
    IF left(@SourceString1= @Delim
    
BEGIN
        
IF @Delim = ' '
            
SET @SourceString = LTRIM(@SourceString)
        
ELSE
            
SET @SourceString = SUBSTRING(
                
@SourceString,
                
PATINDEX('%[^' + @Delim + ']%'@SourceString),
                
LEN(@SourceString- PATINDEX('%[^' + @Delim + ']%'@SourceString+ 1)
    
END
    
-- Right-pad it
    IF right(@SourceString1<> @Delim
        
SET @SourceString = @SourceString + @Delim

    
WHILE @Wordstart > 0
    
BEGIN
        
-- Extract the next word
        SET @WordEND = CHARINDEX(@Delim@SourceString@Wordstart)
        
IF @WordEND > @WordStart
        
BEGIN
            
IF LEN(LTRIM(RTRIM(SUBSTRING(@SourceString@Wordstart@WordEND - @Wordstart)))) > 0
                
INSERT into @Result SELECT SUBSTRING(@SourceString@Wordstart@WordEND - @Wordstart)
            
SET @Wordstart = @WordEND + 1
        
END
        
ELSE SET @Wordstart = 0 -- Terminate the loop
    END

RETURN 
END
GO

GRANT SELECT ON DBO.[fnSplitByDelim] TO PUBLIC
GO
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值