<i id='wFzZd'><tr id='wFzZd'><dt id='wFzZd'><q id='wFzZd'><span id='wFzZd'><b id='wFzZd'><form id='wFzZd'><ins id='wFzZd'></ins><ul id='wFzZd'></ul><sub id='wFzZd'></sub></form><legend id='wFzZd'></legend><bdo id='wFzZd'><pre id='wFzZd'><center id='wFzZd'></center></pre></bdo></b><th id='wFzZd'></th></span></q></dt></tr></i><div id='wFzZd'><tfoot id='wFzZd'></tfoot><dl id='wFzZd'><fieldset id='wFzZd'></fieldset></dl></div>

      <legend id='wFzZd'><style id='wFzZd'><dir id='wFzZd'><q id='wFzZd'></q></dir></style></legend>
      1. <small id='wFzZd'></small><noframes id='wFzZd'>

      2. <tfoot id='wFzZd'></tfoot>

          <bdo id='wFzZd'></bdo><ul id='wFzZd'></ul>
      3. 在 sql server 中拆分字符串

        Splitting the string in sql server(在 sql server 中拆分字符串)
        • <bdo id='OmW9F'></bdo><ul id='OmW9F'></ul>
        • <tfoot id='OmW9F'></tfoot>

            • <i id='OmW9F'><tr id='OmW9F'><dt id='OmW9F'><q id='OmW9F'><span id='OmW9F'><b id='OmW9F'><form id='OmW9F'><ins id='OmW9F'></ins><ul id='OmW9F'></ul><sub id='OmW9F'></sub></form><legend id='OmW9F'></legend><bdo id='OmW9F'><pre id='OmW9F'><center id='OmW9F'></center></pre></bdo></b><th id='OmW9F'></th></span></q></dt></tr></i><div id='OmW9F'><tfoot id='OmW9F'></tfoot><dl id='OmW9F'><fieldset id='OmW9F'></fieldset></dl></div>

                <small id='OmW9F'></small><noframes id='OmW9F'>

                    <tbody id='OmW9F'></tbody>
                1. <legend id='OmW9F'><style id='OmW9F'><dir id='OmW9F'><q id='OmW9F'></q></dir></style></legend>
                2. 本文介绍了在 sql server 中拆分字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我在数据库中有一个以逗号分隔的字符串.比如'苹果、香蕉、菠萝、葡萄'我需要在逗号的基础上拆分这个字符串并遍历这个.由于sql server中没有内置函数,有没有什么有效的方法可以实现这个目标.

                  I have a string in the database which is comma separated.Like 'apple,banana,pineapple,grapes' I need to split this string on the basis of comma and iterate through this.Since there is no built in function in sql server, Is there any efficient way in which this objective can be attained.

                  推荐答案

                  试试这个功能

                  CREATE FUNCTION [dbo].[func_Split] 
                      (   
                      @DelimitedString    varchar(8000),
                      @Delimiter              varchar(100) 
                      )
                  RETURNS @tblArray TABLE
                      (
                      ElementID   int IDENTITY(1,1),  -- Array index
                      Element     varchar(1000)               -- Array element contents
                      )
                  AS
                  BEGIN
                  
                      -- Local Variable Declarations
                      -- ---------------------------
                      DECLARE @Index      smallint,
                                      @Start      smallint,
                                      @DelSize    smallint
                  
                      SET @DelSize = LEN(@Delimiter)
                  
                      -- Loop through source string and add elements to destination table array
                      -- ----------------------------------------------------------------------
                      WHILE LEN(@DelimitedString) > 0
                      BEGIN
                  
                          SET @Index = CHARINDEX(@Delimiter, @DelimitedString)
                  
                          IF @Index = 0
                              BEGIN
                  
                                  INSERT INTO
                                      @tblArray 
                                      (Element)
                                  VALUES
                                      (LTRIM(RTRIM(@DelimitedString)))
                  
                                  BREAK
                              END
                          ELSE
                              BEGIN
                  
                                  INSERT INTO
                                      @tblArray 
                                      (Element)
                                  VALUES
                                      (LTRIM(RTRIM(SUBSTRING(@DelimitedString, 1,@Index - 1))))
                  
                                  SET @Start = @Index + @DelSize
                                  SET @DelimitedString = SUBSTRING(@DelimitedString, @Start , LEN(@DelimitedString) - @Start + 1)
                  
                              END
                      END
                  
                      RETURN
                  END
                  

                  示例用法 – 只需将逗号分隔的字符串以及所需的分隔符传递给函数即可.

                  Example Usage – simply pass the function the comma delimited string as well as your required delimiter.

                  DECLARE @SQLStr varchar(100)
                  SELECT @SQLStr = 'Mickey Mouse, Goofy, Donald Duck, Pluto, Minnie Mouse'
                  
                  SELECT
                      *
                  FROM
                      dbo.func_split(@SQLStr, ',')
                  

                  结果会是这样

                  这篇关于在 sql server 中拆分字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  ibtmp1是非压缩的innodb临时表的独立表空间,通过innodb_temp_data_file_path参数指定文件的路径,文件名和大小,默认配置为ibtmp1:12M:autoextend,也就是说在文件系统磁盘足够的情况下,这个文件大小是可以无限增长的。 为了避免ibtmp1文件无止境的暴涨导致
                  SQL query to group by day(按天分组的 SQL 查询)
                  What does SQL clause quot;GROUP BY 1quot; mean?(SQL 子句“GROUP BY 1是什么意思?意思是?)
                  MySQL groupwise MAX() returns unexpected results(MySQL groupwise MAX() 返回意外结果)
                  MySQL SELECT most frequent by group(MySQL SELECT 按组最频繁)
                  Include missing months in Group By query(在 Group By 查询中包含缺失的月份)

                      <tfoot id='BHYsu'></tfoot>
                          <tbody id='BHYsu'></tbody>
                            <bdo id='BHYsu'></bdo><ul id='BHYsu'></ul>

                            <small id='BHYsu'></small><noframes id='BHYsu'>

                          • <i id='BHYsu'><tr id='BHYsu'><dt id='BHYsu'><q id='BHYsu'><span id='BHYsu'><b id='BHYsu'><form id='BHYsu'><ins id='BHYsu'></ins><ul id='BHYsu'></ul><sub id='BHYsu'></sub></form><legend id='BHYsu'></legend><bdo id='BHYsu'><pre id='BHYsu'><center id='BHYsu'></center></pre></bdo></b><th id='BHYsu'></th></span></q></dt></tr></i><div id='BHYsu'><tfoot id='BHYsu'></tfoot><dl id='BHYsu'><fieldset id='BHYsu'></fieldset></dl></div>

                            <legend id='BHYsu'><style id='BHYsu'><dir id='BHYsu'><q id='BHYsu'></q></dir></style></legend>