当前位置:网站首页>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
边栏推荐
- Qt---error: ‘QObject‘ is an ambiguous base of ‘MyView‘
- What should ABAP do when it calls a third-party API and encounters garbled code?
- AutoCAD - set color
- The vscode waveform curve prompts that the header file cannot be found (an error is reported if the header file exists)
- 程序员怎么才能提高代码编写速度?
- Research Report on market supply and demand and strategy of China's Sodium Tetraphenylborate (cas+143-66-8) industry
- Functional interface, method reference, list collection sorting gadget implemented by lambda
- Explore mongodb - mongodb compass installation, configuration and usage introduction | mongodb GUI
- Market trend report, technical innovation and market forecast of taillight components in China
- Cut! 39 year old Ali P9, saved 150million
猜你喜欢

Qt---error: ‘QObject‘ is an ambiguous base of ‘MyView‘

Anta is actually a technology company? These operations fool netizens
![[North Asia data recovery] data recovery case of database data loss caused by HP DL380 server RAID disk failure](/img/f0/12dd17e840a23dc9ded379e1fd7454.jpg)
[North Asia data recovery] data recovery case of database data loss caused by HP DL380 server RAID disk failure

D3D11_ Chili_ Tutorial (2): draw a triangle

A trap used by combinelatest and a debouncetime based solution

I let the database lock the table! Almost fired!

Principle and general steps of SQL injection

Software Engineer vs Hardware Engineer

Book of night sky 53 "stone soup" of Apache open source community

Ten clothing stores have nine losses. A little change will make you buy every day
随机推荐
Big God explains open source buff gain strategy live broadcast
Go deep into the details of deconstruction and assignment of several data types in JS
What is the catalog of SAP commerce cloud
Communication mode based on stm32f1 single chip microcomputer
Research Report on market supply and demand and strategy of tetramethylpyrazine industry in China
Variable cannot have type 'void'
[book club issue 13] packaging format and coding format of audio files
Research Report on market supply and demand and strategy of China's Sodium Tetraphenylborate (cas+143-66-8) industry
Understand asp Net core - Authentication Based on jwtbearer
Research Report on surgical otorhinolaryngology equipment industry - market status analysis and development prospect prediction
Research Report on market supply and demand and strategy of surgical stapler industry in China
多年锤炼,迈向Kata 3.0 !走进开箱即用的安全容器体验之旅| 龙蜥技术
165 webmaster online toolbox website source code / hare online tool system v2.2.7 Chinese version
System.currentTimeMillis() 和 System.nanoTime() 哪个更快?别用错了!
Using celery in projects
Can I "reverse" a Boolean value- Can I 'invert' a bool?
I let the database lock the table! Almost fired!
Qt---error: ‘QObject‘ is an ambiguous base of ‘MyView‘
The new generation of domestic ORM framework sagacity sqltoy-5.1.25 release
Unity script API - GameObject game object, object object