【51CTO/BBS】请教: SQL里有没有字符串分解Split的函数??

本文涉及的产品
云数据库 RDS SQL Server,基础系列 2核4GB
简介:

原帖地址:http://bbs.51cto.com/thread-1133863-1.html

 

问题描述:



VB 中有两个非常好用的字符串处理函数:

Split(字符串,分隔符)作用:将【字符串】以【分隔符】作为边界,分解成数组。 返回:一个字符串数组。

Join(字符数组,分隔符)作用:将【字符数组】中的元素,以【分隔符】作为边界,连接成一个字符串。返回:一个字符串。

请教老师们,SQL里是否有类似的函数?

 

解决方案:



如何用SQL Server Function实现Split


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
--Split 表函数将一个字符串按指定分隔符进行分割,返回一个表。
create  function  split(
@string  varchar (255), --待分割字符串
@separator  varchar (255) --分割符
) returns  @array  table (item  varchar (255))
as
begin
declare  @ begin  int ,@ end  int ,@item  varchar (255)
set  @ begin  = 1
set  @ end =charindex(@separator,@string,@ begin )
while(@ end <>0)
begin
set  @item =  substring (@string,@ begin ,@ end -@ begin )
insert  into  @array(item)  values (@item)
set  @ begin  = @ end +1
set  @ end =charindex(@separator,@string,@ begin )
end
set  @item =  substring (@string,@ begin ,len(@string)+1-@ begin )
if (len(@item)>0)
insert  into  @array(item)  values ( substring (@string,@ begin ,len(@string)+1-@ begin ))
return
end

 

如何用SQL CLR实现Split



步骤一:

 

开始,运行Visual Studio 2012,选择“New Project”,选择“Visual C#”,“类库”,命名类库为fnSplit。

 

clip_image001

 

步骤二:

 

默认,Visual studio创建一个空的类命名为“Class1.cs”,右键重命名为CLRFunction.cs。

 

clip_image002

 

步骤三:

 

双击“CLRFunction.cs”文件,输入如下代码:

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
using  System;
using  System.Collections;
using  System.Text;
using  Microsoft.SqlServer.Server;
using  System.Data.SqlTypes;
namespace  fnSplit
{
public  static  class  CLRFunctions
{
//SQL Functions require an additional "SqlFunction" Attribute.
//This attribute provides SQL server with additional meta data information it needs
//regarding our custom function. In this example we are not accessing any data, and our
//function is deterministic. So we let SQL know those facts about our function with
//the DataAccess and IsDeterministic parameters of our attribute.
//Additionally, SQL needs to know the name of a function it can defer to when it needs
//to convert the object we have returned from our function into a structure that SQL
//can understand. This is provided by the "FillRowMethodName" shown below.
[SqlFunction(
DataAccess = DataAccessKind.None,
FillRowMethodName =  "MyFillRowMethod"
,IsDeterministic= true )
]
//SQL Functions must be declared as Static. Table Valued functions must also
//return a class that implements the IEnumerable interface. Most built in
//.NET collections and arrays already implement this interface.
public  static  IEnumerable Split( string  stringToSplit,  string  delimiters)
{
//One line of C# code splits our string on one or more delimiters...
//A string array is one of many objects that are returnable from
//a SQL CLR function - as it implements the required IEnumerable interface.
string [] elements = stringToSplit.Split(delimiters.ToCharArray());
return  elements;
}
//SQL needs to defer to user code to translate the an IEnumerable item into something
//SQL Server can understand. In this case we convert our string to a SqlChar object...
public  static  void  MyFillRowMethod(Object theItem,  out  SqlChars results)
{
results =  new  SqlChars(theItem.ToString());
}
}
}

 

步骤四:

 

从BUILD菜单,选择“Build fnSplit”。编译后,在bin目录生成“fnSplit.dll”文件。拷贝该文件到SQL Server可访问目录,如D:\MSSQL\DATA\CLRLibraries。

 

clip_image003

 

clip_image004

 

步骤五:

 

打开SQL Server Management Studio,连接到需要部署该DLL的实例。

 

clip_image005

 

步骤六:

 

CLR集成默认在SQL Server是禁用的。执行下面的命令启用CRL集成。

 

1
2
3
4
5
6
7
8
9
sp_configure  'show advanced options' , 1
RECONFIGURE
GO
sp_configure  'clr enabled' , 1
RECONFIGURE
GO
sp_configure  'show advanced options' , 0
RECONFIGURE
GO

 

步骤七:

 

在应用的数据库中通过该DLL创建Assemblies。

 

1
2
Create  Assembly fnSplit  from  'D:\MSSQL\DATA\CLRLibraries\fnSplit.dll'  with  Permission_set = SAFE
GO

 

clip_image006

 

步骤八:

 

创建Split函数,语法类似创建标准函数,除了使用“External”定位实际的程序逻辑到你的DLL中。

 

1
2
3
4
5
6
7
Create  Function  fnSplit(@StringToSplit nvarchar( max ), @splitOnChars nvarchar( max ) )
returns  Table  (
Results nvarchar( max )
)
AS
External  name  fnSplit.[fnSplit.CLRFunctions].Split;
GO

 

clip_image007

 

步骤九:

 

测试Split函数

 

1
SELECT  FROM  dbo.fnSplit( '1,2,3,4,5:6:7~8~9' , ',:~' )

 

clip_image008



















本文转自UltraSQL51CTO博客,原文链接:http://blog.51cto.com/ultrasql/1593531 ,如需转载请自行联系原作者









相关文章
|
5月前
|
SQL Java 数据库连接
MyBatis动态SQL字符串空值判断,这个细节99%的程序员都踩过坑!
本文深入探讨了MyBatis动态SQL中字符串参数判空的常见问题。通过具体案例分析,对比了`name != null and name != &#39;&#39;`与`name != null and name != &#39; &#39;`两种写法的差异,指出后者可能引发逻辑混乱。为避免此类问题,建议在后端对参数进行预处理(如trim去空格),简化MyBatis判断逻辑,提升代码健壮性与可维护性。细节决定成败,严谨处理参数判空是写出高质量代码的关键。
562 0
|
1月前
|
SQL 人工智能 数据挖掘
如何在`score`表中正确使用`COUNT`和`AVG`函数?SQL聚合函数COUNT与AVG使用指南
本文三桥君通过score表实例解析SQL聚合函数COUNT和AVG的常见用法。详解COUNT(studentNo)、COUNT(score)、COUNT()的区别,以及AVG函数对数值/字符型字段的不同处理,特别指出AVG()是无效语法。实战部分提供6个典型查询案例及结果,包含创建表、插入数据的完整SQL代码。产品专家三桥君强调正确理解函数特性(如空值处理、字段类型限制)对数据分析的重要性,帮助开发者避免常见误区,提升查询效率。
121 0
|
11月前
|
SQL Oracle 关系型数据库
SQL优化-使用联合索引和函数索引
在一次例行巡检中,发现一条使用 `to_char` 函数将日期转换为字符串的 SQL 语句 CPU 利用率很高。为了优化该语句,首先分析了 where 条件中各列的选择性,并创建了不同类型的索引,包括普通索引、函数索引和虚拟列索引。通过对比不同索引的执行计划,最终确定了使用复合索引(包含函数表达式)能够显著降低查询成本,提高执行效率。
139 3
|
11月前
|
SQL 数据库 数据库管理
数据库SQL函数应用技巧与方法
在数据库管理中,SQL函数是处理和分析数据的强大工具
|
11月前
|
SQL 数据库 索引
SQL中COUNT函数结合条件使用的技巧与方法
在SQL查询中,COUNT函数是一个非常常用的聚合函数,用于计算表中满足特定条件的记录数
1981 5
|
11月前
|
SQL 关系型数据库 MySQL
SQL日期函数
SQL日期函数
163 0
|
11月前
|
SQL Oracle 关系型数据库
SQL语句中的引号使用技巧:正确处理字符串与标识符
在编写SQL语句时,引号的使用是一个基础且重要的环节
1434 0
|
12月前
|
SQL 关系型数据库 MySQL
MySQL根据某个字段包含某个字符串或者字段的长度情况更新另一个字段的值,如何写sql
MySQL根据某个字段包含某个字符串或者字段的长度情况更新另一个字段的值,如何写sql
556 0
|
SQL
SQL点滴33—SQL中的字符串操作
原文:SQL点滴33—SQL中的字符串操作 计算字符串长度len()用来计算字符串的长度 select sname ,len(sname) from student 字符串转换为大、小写lower() 用来将一个字符串转换为小写,upper() 用来将一个字符串转换为大写 sele...
884 0
|
SQL
SQL点滴33—SQL中的字符串操作
计算字符串长度len()用来计算字符串的长度 select sname ,len(sname) from student 字符串转换为大、小写lower() 用来将一个字符串转换为小写,upper() 用来将一个字符串转换为大写 select lower('I AM A STUDE...
786 0

热门文章

最新文章