SQL SplitString function

简介: /*while set ANSI_NULLS on, select * from table1 where column_name1=NULL will return 0 row, select * from table1 where column_...

/*while set ANSI_NULLS on, select * from table1 where column_name1=NULL will return 0 row, select * from table1 where column_name1<>NULL will return 0 row, no matter column_name1 contains null value or not.*/ /*while set ANSI_NULLS off, select * from table1 where column_name1=NULL will return null rows, select * from table1 where column_name1<>NULL will return not null rows, select * from table1 where column_name1<>value1 will return not equal value1 and not null.*/ SET ANSI_NULLS ON GO /*while set QUOTED_IDENTIFIER on, Identifiers can be delimited by double quotation marks, and text must be delimited by single quotes.*/ /*while set QUOTED_IDENTIFIER off, Identifier is not enclosed in quotes, and must comply with all Transact-SQL rules for identifiers.*/ SET QUOTED_IDENTIFIER ON GO CREATE FUNCTION [dbo].[fn_SplitString] ( @String NVARCHAR(2000), @Delimiter NVARCHAR(1) ) RETURNS @ValueTable TABLE ([Value] NVARCHAR(2000), [Index] INT) BEGIN DECLARE @NextString NVARCHAR(2000) DECLARE @Pos INT DECLARE @NextPos INT DECLARE @Index INT DECLARE @LastChar NVARCHAR(1) SET @String = RTRIM(LTRIM(@String)) SET @NextString = '' SET @Index = 0 SET @LastChar = RIGHT(@String,1) IF (@LastChar <> @Delimiter ) BEGIN SET @String = @String + @Delimiter END SET @Pos = CHARINDEX(@Delimiter, @String) SET @NextPos = 1 WHILE (@Pos <> 0) BEGIN SET @NextString = SUBSTRING(@String, 1, @Pos - 1) SET @NextString = RTRIM(LTRIM(@NextString)) INSERT INTO @ValueTable ( [Value], [Index]) VALUES (@NextString, @Index) SET @String = SUBSTRING(@String, @Pos + 1, LEN(@String)) SET @NextPos = @Pos SET @Pos = CHARINDEX(@Delimiter, @String) SET @Index = @Index + 1 END RETURN END

目录
打赏
0
0
0
0
20
分享
相关文章
【Azure 应用服务】Azure JS Function 异步方法中执行SQL查询后,Callback函数中日志无法输出问题
【Azure 应用服务】Azure JS Function 异步方法中执行SQL查询后,Callback函数中日志无法输出问题
105 0
|
9月前
|
SQL MIN() Function
SQL MIN() Function
54 6
SQL编程【MySQL 01】拆分列字段为行并根据类型翻译字段 > 1305 - FUNCTION x.help_topic_id does not exist 报错问题
SQL编程【MySQL 01】拆分列字段为行并根据类型翻译字段 > 1305 - FUNCTION x.help_topic_id does not exist 报错问题
115 0
|
10月前
|
SQL
Greenplum【SQL 03】实现树结构+自定义函数+避免函数重复调用+ function cannot execute on a QE slice 问题处理(优化过程全记录)
Greenplum【SQL 03】实现树结构+自定义函数+避免函数重复调用+ function cannot execute on a QE slice 问题处理(优化过程全记录)
194 0
MySQL:ERROR 1418 (HY000): This function has none of DETERMINISTIC, NO SQL, or READS SQL DATA
MySQL:ERROR 1418 (HY000): This function has none of DETERMINISTIC, NO SQL, or READS SQL DATA
258 0
Using Python to Connect Function Compute to SQL Server
Normally, a third-party module is required for establishing Python-based database connections. To connect to Microsoft SQL Server, pymssql is required.
12752 0
Oracle On the PL/SQL Function Result Cache
标签 PostgreSQL , Oracle , 函数结果缓存 , 函数三态 , immutable , stable , volatile 背景 Oracle 11g 支持的一个新特性,在创建PL/SQL函数时,可以指定这个存储过程是否需要对结果进行缓存,缓存内容在SGA内存区域。
985 0
SQL SERVER中用户定义标量函数(scalar user defined function)的性能问题
原文:SQL SERVER中用户定义标量函数(scalar user defined function)的性能问题 用户定义函数(UDF)分类       SQL SERVER中的用户定义函数(User Defined Functions 简称UDF)分为标量函数(Scalar-Valued Function)和表值函数(Table-Valued Function)。
1278 0
SQL Server 自定义函数(Function)——参数默认值
原文:SQL Server 自定义函数(Function)——参数默认值 sql server 自定义函数分为三种类型:标量函数(Scalar Function)、内嵌表值函数(Inline Function)、多声明表值函数(Multi-Statement Function) 标量函数:标量函数是对单一值操作,返回单一值。
1990 0

热门文章

最新文章

AI助理

你好,我是AI助理

可以解答问题、推荐解决方案等