SQL Server gets the length after separating the string

Source: Internet
Author: User

--方法1

--sql 分隔字符串,返回个数 CREATE function f_splitLen_1 (    @str  varchar (1024),   --要分割的字符串    @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)    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 --演示1 select dbo.f_splitLen_1( ‘1,2,3,4‘ , ‘,‘ as ‘Count‘ -- 查询结果为:4 create function f_splitLen_2 (      @str  varchar (1024),   --要分割的字符串      @split  varchar (10)   --分隔符号 ) returns int as begin      return len(@str)-len( replace (@str,@split, ‘‘ ))+1    end        --演示2 select dbo.f_splitLen_2( ‘1,2,3,4‘ , ‘,‘ as ‘Count‘ -- 查询结果为:4

SQL Server gets the length after separating the string

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.