当前位置:网站首页>Sql实现Split
Sql实现Split
2022-07-04 14:54:00 【全栈程序员站长】
create function [dbo].[Get_StrArrayLength] ( @str varchar(max), –要分割的字符串 @split varchar(10) –分隔符号 ) returns int as begin declare @location int declare @start int declare @length int –去掉前后空格 set @str=ltrim(rtrim(@str)) –分隔符第一次出现的位置 set @location=charindex(@split,@str) –设置分隔后的字符串长度为1 set @length=1
while @location<>0 begin set @[email protected]+1 set @location=charindex(@split,@str,@start) set @[email protected]+1 end return @length end
drop function [dbo].[Get_StrArrayStrOfIndex] go create function [dbo].[Get_StrArrayStrOfIndex] ( @str varchar(max), –要分割的字符串 @split varchar(10), –分隔符号 @index int –取第几个元素 ) returns varchar(1024) as begin declare @location int declare @start int declare @next int declare @seed int declare @pos int if @index<=0 return ” set @str=ltrim(rtrim(@str)) set @start=1 set @next=1 set @pos=1 set @seed=len(@split) set @location=charindex(@split,@str) while @location<>0 and @index>@next and @pos<@index begin set @[email protected][email protected] set @location=charindex(@split,@str,@start) set @[email protected]+1 set @[email protected]+1 end if @location =0 if @pos<>@index return ” else select @location =len(@str)+1 –这儿存在两种情况:1、字符串不存在分隔符号 2、字符串中存在分隔符号,跳出while循环后,@location为0,那默认为字符串后边有一个分隔符号。 return substring(@str,@start,@[email protected]) end
declare @id varchar(30) select @id=dbo.Get_StrArrayStrOfIndex(‘4321,2223,333,5554′,’,’,2)
我的改进版本,可像Hashtable一样访问
drop function [dbo].[Get_ArrayStrOfName] Go
create function [dbo].[Get_ArrayStrOfName] ( @str varchar(5000), –要分割的字符串 @split varchar(1), –分隔符号 @itemSplit varchar(1), –元素分割符 @item varchar(20) –取哪个元素 ) returns varchar(1024) As Begin declare @location int –找到子串的位置 declare @tempItem varchar(50) –当前项 declare @tempItemName varchar(20) –当前项的名称 set @str=ltrim(rtrim(@str)) set @tempItem=” set @tempItemName=” while len(@str)>0 Begin —–找到一个子项– set @location=charindex(@split,@str,1) if @location>0 Begin set @tempItem=substring(@str,1,@location-1) set @str=substring(@str,@location+1,len(@str)) End Else Begin set @[email protected] set @str=” end ———-判断当前子项的名称————– set @location=charindex(@itemSplit,@tempItem,1) if @location>0 Begin set @tempItemName=substring(@tempItem,1,@location-1) if @[email protected] return substring(@tempItem,@location+1,len(@tempItem)) End End return ” End Go
declare @id varchar(30) select @id=dbo.Get_ArrayStrOfName(‘[email protected],[email protected],[email protected],[email protected]’, ‘,’, ‘@’, ‘ff’) print @id
发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/110915.html原文链接:https://javaforall.cn
边栏推荐
- Selenium element interaction
- Will the memory of ParticleSystem be affected by maxparticles
- Preliminary practice of niuke.com (10)
- Using celery in projects
- C language: implementation of daffodil number function
- ~88 running people practice
- Intranet penetrating FRP: hidden communication tunnel technology
- Market trend report, technical innovation and market forecast of tetrabromophthalate (pht4 diol) in China
- Vscode prompt Please install clang or check configuration 'clang executable‘
- Audio and video technology development weekly | 252
猜你喜欢
Principle and general steps of SQL injection
QT graphical view frame: element movement
~88 running people practice
~89 deformation translation
Actual combat | use composite material 3 in application
Blood cases caused by Lombok use
Nine CIO trends and priorities in 2022
How to decrypt worksheet protection password in Excel file
《吐血整理》保姆级系列教程-玩转Fiddler抓包教程(2)-初识Fiddler让你理性认识一下
What is torch NN?
随机推荐
Blood cases caused by Lombok use
2022年九大CIO趨勢和優先事項
China's plastic processing machinery market trend report, technological innovation and market forecast
Talking about Net core how to use efcore to inject multiple instances of a context annotation type for connecting to the master-slave database
Cut! 39 year old Ali P9, saved 150million
. Net delay queue
Daily notes~
Accounting regulations and professional ethics [7]
D3D11_ Chili_ Tutorial (2): draw a triangle
Find numbers
Unity prefab day04
China's roof ladder market trend report, technological innovation and market forecast
What encryption algorithm is used for the master password of odoo database?
Talking about Net core how to use efcore to inject multiple instances of a context annotation type for connecting to the master-slave database
Market trend report, technical innovation and market forecast of electrochromic glass and devices in China and Indonesia
嵌入式软件架构设计-函数调用
c# 实现定义一套中间SQL可以跨库执行的SQL语句
Final consistency of MESI cache in CPU -- why does CPU need cache
Socks agent tools earthworm, ssoks
165 webmaster online toolbox website source code / hare online tool system v2.2.7 Chinese version