【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 ,如需转载请自行联系原作者









相关实践学习
使用SQL语句管理索引
本次实验主要介绍如何在RDS-SQLServer数据库中,使用SQL语句管理索引。
SQL Server on Linux入门教程
SQL Server数据库一直只提供Windows下的版本。2016年微软宣布推出可运行在Linux系统下的SQL Server数据库,该版本目前还是早期预览版本。本课程主要介绍SQLServer On Linux的基本知识。 相关的阿里云产品:云数据库RDS&nbsp;SQL Server版 RDS SQL Server不仅拥有高可用架构和任意时间点的数据恢复功能,强力支撑各种企业应用,同时也包含了微软的License费用,减少额外支出。 了解产品详情:&nbsp;https://www.aliyun.com/product/rds/sqlserver
相关文章
|
2天前
|
SQL 关系型数据库 MySQL
SQL脚本字符串替换
【5月更文挑战第3天】
19 4
|
2天前
T-sql 高级查询( 5*函数 联接 分组 子查询)
T-sql 高级查询( 5*函数 联接 分组 子查询)
|
2天前
|
SQL 关系型数据库 MySQL
MYSQL数字函数实操宝典:场景化SQL语句一网打尽
本文作为MYSQL数字函数实操宝典,通过丰富的场景化SQL语句实例,一网打尽了MYSQL中常用的数字函数。我们深入探讨了ROUND函数在金额四舍五入、评分计算等场景的应用,展现了其在确保数据准确性和展示规范性方面的重要性。同时,FLOOR函数在分页处理、价格计算等实际案例中的灵活运用,也体现了其在向下取整方面的优势。此外,CEIL函数在分页处理、费用计算等方面的应用案例,充分展示了其向上取整的功能特点。这些实用案例不仅有助于读者更好地理解MYSQL数字函数的工作原理,更为开发者在实际开发中提供了有价值的参考
46 8
|
2天前
|
SQL 存储 Python
Microsoft SQL Server 编写汉字转拼音函数
Microsoft SQL Server 编写汉字转拼音函数
|
2天前
|
SQL 存储 Apache
在 Apache Flink SQL 中,并没有内置的 GROUP_CONCAT 函数
【2月更文挑战第16天】在 Apache Flink SQL 中,并没有内置的 GROUP_CONCAT 函数
219 2
|
2天前
|
SQL 存储
SQL Server基本函数
SQL Server基本函数
|
SQL 存储 数据库
SQL Server函数与存储过程 计算时间
SQL Server函数与存储过程 计算时间 一、通过一个开始时间、结束时间计算出一个工作日天数(不包含工作日与节假日);   1、函数 --创建函数,参数 @bengrq 开始时间,@endrq 结束时间 create function [dbo].
1732 0
|
SQL 存储 Perl
PL/SQL函数和存储过程
前言 活到老,学到老。 基本概念 --ORACLE 提供可以把PL/SQL 程序存储在数据库中,并可以在任何地方来运行它。这样就叫存储过程或函数。过程和函数统称为PL/SQL子程序,他们是被命名的PL/SQL块,均存储在数据库中,并通过输入、输出参数或输入/输出参数与其调用者交换信息。
1365 0