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

目录
相关文章
|
22天前
|
SQL 数据库 数据处理
什么是 ABAP SQL Function
什么是 ABAP SQL Function
10 0
什么是 ABAP SQL Function
|
存储 SQL 负载均衡
SQL 函数 function 讲解+代码实例
SQL 函数 function 讲解+代码实例
SQL 函数 function 讲解+代码实例
|
SQL 存储
使用 SAP CDS view SQL Function 将视图某些字段进行合并
使用 SAP CDS view SQL Function 将视图某些字段进行合并
132 0
使用 SAP CDS view SQL Function 将视图某些字段进行合并
|
SQL Go 数据库
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)。
1218 0
|
SQL Go 数据库
SQL Server 自定义函数(Function)——参数默认值
原文:SQL Server 自定义函数(Function)——参数默认值 sql server 自定义函数分为三种类型:标量函数(Scalar Function)、内嵌表值函数(Inline Function)、多声明表值函数(Multi-Statement Function) 标量函数:标量函数是对单一值操作,返回单一值。
1845 0
|
SQL Oracle 关系型数据库
在Oracle/SQL Service中通过Function返回Table
本函数用途:返回一个Table 在Oracle中实现,范例: 1 --在Types中: 2 create or replace type objTable as object 3 ( 4 s_usercode varchar2(32767), 5 s_usernam...
816 0
|
SQL C# Go
sql and csharp: Split Function
T-SQL:   declare @int int,@prov int,@city int,@str nvarchar(500) set @str='天河麗特青春:中國廣東省廣州市天河區天河路623號天河娛樂廣場麗特青春百貨一樓,塗聚文' select @int=charindex(':',@str) select @prov=charindex('省',@str) sel
991 0
|
SQL .NET 开发框架
ASP上两个防止SQL注入式攻击Function
 ''==========================''过滤提交表单中的SQL''==========================function ForSqlForm()dim fqys,errc,i,itemsdim nothis(18) nothis(0)="net user" n...
712 0