/*
Microsoft SQL Server 2017 (RTM-CU5) (KB4092643) - 14.0.3023.8 (X64)   Mar  2 2018 18:24:44
Copyright (C) 2017 Microsoft Corporation  Enterprise Edition: Core-based Licensing (64-bit) on
Windows 10 Pro 10.0 <X64> (Build 15063: )
*/
--备用函数
if object_id('getnums') is not null drop function getnums
go
create function [dbo].[getnums]
(@n int)
returns table as
return
with t1 as(select 1 n union all select 1)
,t2 as(select 1 n from t1,t1 a,t1 b,t1 c)
,t3 as(select 1 n from t2,t2 a,t2 b,t2 c)
,t4 as(select 1 n from t3,t3 a)
select top(@n) row_number()over(order by(select 1)) n from t4 order by n
go
--1. 基于 xml path + row_number
if object_id('fn_split') is not null drop function fn_split
go
create function fn_split(@s nvarchar(max),@split nvarchar(2))
returns table
as
return
with
t0 as (select substring(@s,n,1)ch,n from getnums(len(@s)))
,
t1 as(select ch,rid=n-row_number()over(order by n)+1 from t0 where ch<>@split)
select
rid,
keys=(select  ''+ch from t1 b where b.rid=a.rid for xml path(''))
from t1 a
group by rid
go
--2. 基于 xml 的nodes
IF OBJECT_ID('[dbo].[Fun_String2ToStringArray]') IS NOT NULL DROP FUNCTION [dbo].[Fun_String2ToStringArray]
GO
CREATE FUNCTION [dbo].[Fun_String2ToStringArray](@str NVARCHAR(MAX), @split NVARCHAR(10))
RETURNS @table TABLE ([item] NVARCHAR(max))
AS
BEGINIF LEN(@split) = 0BEGINSET @split = N','ENDDECLARE @xml XML;SET @xml = CONVERT(XML, '<x><![CDATA[' + replace(CONVERT(VARCHAR(MAX), @str), @split, ']]></x><x><![CDATA[') + ']]></x>')INSERT INTO @tableSELECT itemFROM   (SELECT c.value('text()[1]', 'nvarchar(4000)') [item]FROM   @xml.nodes('/x') t(c)) tWHERE  item IS NOT NULLRETURN
END
GO
--3. 从SQL Server2016起支持的分割表值函数 string_split , 不用写--4. 下面是测试代码
DECLARE @str NVARCHAR(MAX);
SELECT @str='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,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80,81,82,83,84,85,86,87,88,89,90,91,92,93,94,95,96,97,98,99,100,101,102,103,104,105,106,107,108,109,110,111,112,113,114,115,116,117,118,119,120,121,122,123,124,125,126,127,128,129,130,131,132,133,134,135,136,137,138,139,140,141,142,143,144,145,146,147,148,149,150,151,152,153,154,155,156,157,158,159,160,161,162,163,164,165,166,167,168,169,170,171,172,173,174,175,176,177,178,179,180,181,182,183,184,185,186,187,188,189,190,191,192,193,194,195,196,197,198,199,200,201,202,203,204,205,206,207,208,209,210,211,212,213,214,215,216,217,218,219,220,221,222,223,224,225,226,227,228,229,230,231,232,233,234,235,236,237,238,239,240,241,242,243,244,245,246,247,248,249,250,251,252,253,254,255,256,257,258,259,260,261,262,263,264,265,266,267,268,269,270,271,272,273,274,275,276,277,278,279,280,281,282,283,284,285,286,287,288,289,290,291,292,293,294,295,296,297,298,299,300,301,302,303,304,305,306,307,308,309,310,311,312,313,314,315,316,317,318,319,320,321,322,323,324,325,326,327,328,329,330,331,332,333,334,335,336,337,338,339,340,341,342,343,344,345,346,347,348,349,350,351,352,353,354,355,356,357,358,359,360,361,362,363,364,365,366,367,368,369,370,371,372,373,374,375,376,377,378,379,380,381,382,383,384,385,386,387,388,389,390,391,392,393,394,395,396,397,398,399,400,401,402,403,404,405,406,407,408,409,410,411,412,413,414,415,416,417,418,419,420,421,422,423,424,425,426,427,428,429,430,431,432,433,434,435,436,437,438,439,440,441,442,443,444,445,446,447,448,449,450,451,452,453,454,455,456,457,458,459,460,461,462,463,464,465,466,467,468,469,470,471,472,473,474,475,476,477,478,479,480,481,482,483,484,485,486,487,488,489,490,491,492,493,494,495,496,497,498,499,500';SET NOCOUNT ON
SET STATISTICS IO ON
SET STATISTICS TIME ONSELECT * FROM dbo.fn_split(@str,',') AS fsSELECT * FROM dbo.[Fun_String2ToStringArray](@str,',') AS fsSELECT * FROM string_split(@str,',') AS fs
/*
SQL Server 分析和编译时间: CPU 时间 = 0 毫秒,占用时间 = 0 毫秒。SQL Server 执行时间:CPU 时间 = 0 毫秒,占用时间 = 0 毫秒。SQL Server 执行时间:CPU 时间 = 0 毫秒,占用时间 = 0 毫秒。SQL Server 执行时间:CPU 时间 = 0 毫秒,占用时间 = 0 毫秒。SQL Server 执行时间:CPU 时间 = 0 毫秒,占用时间 = 0 毫秒。
表 'Worktable'。扫描计数 0,逻辑读取 0 次,物理读取 0 次,预读 0 次,lob 逻辑读取 0 次,lob 物理读取 0 次,lob 预读 0 次。SQL Server 执行时间:CPU 时间 = 1187 毫秒,占用时间 = 1204 毫秒。
表 '#BD909E0C'。扫描计数 1,逻辑读取 2 次,物理读取 0 次,预读 0 次,lob 逻辑读取 0 次,lob 物理读取 0 次,lob 预读 0 次。SQL Server 执行时间:CPU 时间 = 0 毫秒,占用时间 = 181 毫秒。SQL Server 执行时间:CPU 时间 = 0 毫秒,占用时间 = 3 毫秒。
*/
/******************************************** 结论:* string_split             最佳* Fun_String2ToStringArray 其次* fn_split                 第三*******************************************/

字符串分割表值函数评测相关推荐

  1. 拆分字符串的表值函数

    1--拆分字符串的表值函数  2  3alter Function f_Split  4(  5    @Str Nvarchar(max)  6)Returns @Re Table  7(  8   ...

  2. SQL 字符串分割表函数

    1 --字符串分割表函数 2 declare @str varchar(1000) 3 declare @split varchar(10) 4 5 declare @i int; 6 declare ...

  3. MySQL 字符串分割 SUBSTRING_INDEX函数

    From: MySQL 字符串分割 SUBSTRING_INDEX函数 Sql代码 SUBSTRING_INDEX(str,delim,count) 用delim 分割str,取第count个子串 u ...

  4. php支持中文字符串分割的函数

    <?php/*** str_split不支持中文,利用mb_xx函数实现个* 2个用哪个都成* */ $str = "月日上午湖北荆州安良百货商场内一名岁的女子被搅入手扶电梯身亡据广西 ...

  5. Oracle拆分字符串,字符串分割的函数。

    第一种:oracle字符串分割和提取 分割 create or replace function Get_StrArrayLength (av_str varchar2, --要分割的字符串av_sp ...

  6. 字符串分割 strsep 函数

    原型:char *strsep(char **stringp, const char *delim); 功能:分解字符串为一组字符串.从stringp指向的位置起向后扫描,遇到delim指向的字符串中 ...

  7. php字符串分割汉字,php支持中文字符串分割的函数

    本文给大家分享了2个php使用mb_xxx方法来实现中文字符分割的方法,其基本思路都差不多,有需要的小伙伴可以参考下. str_split不支持中文,利用mb_xx函数实现个 /** * Conver ...

  8. php 汉字分割,php支持中文字符串分割的函数

    str_split不支持中文,利用mb_xx函数实现个 /** * Convert a string to an array * @param string $str * @param number ...

  9. C++实现字符串分割函数split()

    目录 使用strtok()完成分割 使用strsep()完成分割 使用strtok_r()完成分割 实现字符串分割 前言 最近遇到了一个字符串分割的问题,在C++的标准库里面没有字符分割函数split ...

最新文章

  1. 【学习笔记】36、lambda是单表达式函数
  2. linux mint 蓝牙,Linuxmint19蓝牙连接的问题
  3. Bootstrap全局css样式_表格
  4. python 相对导入_python 相对导入与绝对导入
  5. 淘宝网商品管理?技术 ?
  6. java的静态如何理解_java中的静态是什么?如何理解?
  7. apache poi使用例_使用java Apache poi 根据word模板生成word报表例子
  8. html5控制gif图的播放和暂停,使用JS和canvas实现gif动图的停止和播放代码
  9. python把print写入文件_python print输出到文件
  10. 你我贷CTO冯炯:互联网金融的P2P+O2O怎么做?
  11. 豆丁 道客巴巴 千图网 千库网 包图网 我图网 摄图网 巧办网 万方设计 中国知网 VIP会员一个月开通方法,十网通用
  12. 高速串行收发器的预加重与均衡
  13. Centos8 更换DNF源
  14. 【记忆化搜索/数位DP】zznu2175(长度为n的含有ACM的字符串)
  15. 【Spring Boot】构造、访问Restful Webservice与定时任务
  16. Ubuntu 18.04 LTS (Bionic Beaver) 已经发布附官网下载链接
  17. I DID IT 推广二番 | ArcBlock 发布汇款转账应用 Demo
  18. 定期存款遇调息怎么处理?
  19. 一个保护眼睛的小技巧
  20. JQuery 查询文档元素

热门文章

  1. 短信验证码—Java实现
  2. 写个贺词,祝贺销售额业绩突破千万,要引经据典,对未来有所展望。
  3. mac软件linux系统,Linux/MAC OSX 开机程序
  4. Unity3D-高通AR-《狼来了》-4AR场景中的UI交互
  5. MATLAB图像的阈值变换
  6. 让你快速了解Ngnix
  7. 300大作战服务器维修,300大作战官方服
  8. Nao6 - Nao Recode 录音
  9. 高级程序员的应用技巧之:HTML 速查列表的使用
  10. 逆向分析中的密码学---tea