In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)05/31 Report--
Today, I will talk to you about how to use the SplitString function in SQLServer. Many people may not know much about it. In order to make you understand better, the editor has summarized the following for you. I hope you can get something according to this article.
The code is as follows: SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO / * by kudychen 2011-9-28 * / CREATE function [dbo]. [SplitString] (@ Input nvarchar (max),-- input string to be separated @ Separator nvarchar (max) =',',-- a string that delimit the substrings in the input string @ RemoveEmptyEntries bit=1-- the return value does not include array elements that contain an empty string) returns @ TABLE table ([Id] int identity (1), [Value] nvarchar (max)) as begin declare @ Index int @ Entry nvarchar (max) set @ Index = charindex (@ Separator,@Input) while (@ Index > 0) begin set @ Entry=ltrim (rtrim (substring (@ Input, 1, @ Index-1)) if (@ RemoveEmptyEntries=0) or (@ RemoveEmptyEntries=1 and @ Entry'') begin insert into @ TABLE ([Value]) Values (@ Entry) end set @ Input = substring (@ Input, @ Index+datalength (@ Separator) / 2, len (@ Input)) set @ Index = charindex (@ Separator) @ Input) end set @ Entry=ltrim (rtrim (@ Input)) if (@ RemoveEmptyEntries=0) or (@ RemoveEmptyEntries=1 and @ Entry'') begin insert into @ TABLE ([Value]) Values (@ Entry) end return end
How to use:
The code is as follows: declare @ str1 varchar (max), @ str2 varchar (max), @ str3 varchar (max) set @ str1 = '1Power2 set @ str2 =' 1 'set @ str3 =' 1 'Value from [dbo]. [SplitString] (@ str1,',', 1) select [Value] from [dbo]. [SplitString] (@ str2,'# #'') 1) select [Value] from [dbo]. [SplitString] (@ str3,'# #', 0)
Execution result:
There is also a self-added [Id] field, which may be used in some cases, such as saving sorting according to Id, and so on. For example, save the sort according to the ID of a table: copy the code as follows: update a set a. [Order] = t.[ ID] from [dbo]. [table] as a join [dbo] .SplitString ('1meme 2dhow3,',', 1) as t on a. [Id] = t.[ value]
After reading the above, do you have any further understanding of how to use the SplitString function in SQLServer? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.