求sql中某列含逗号的个数
如某列为
1
1,2
1,22,3,33
则查询出结果为:
0
1
3

解决方案 »

  1.   

    select len(Col1)-len(replace([Col1]),',','') from tablename
      

  2.   

    select 
    len(col)-len(replace(col,',',''))+1
    from tb
      

  3.   

    select len(col)-len(replace(col,',','')) from tb;
      

  4.   

    ----------------------------------------------------------------
    -- Author  :fredrickhu(我是小F,向高手学习)
    -- Date    :2010-01-25 11:04:59
    -- Version:
    --      Microsoft SQL Server 2005 - 9.00.4035.00 (Intel X86) 
    -- Nov 24 2008 13:01:59 
    -- Copyright (c) 1988-2005 Microsoft Corporation
    -- Developer Edition on Windows NT 5.2 (Build 3790: Service Pack 1)
    --
    ----------------------------------------------------------------
    --> 测试数据:[tb]
    if object_id('[tb]') is not null drop table [tb]
    go 
    create table [tb]([col] varchar(9))
    insert [tb]
    select '1' union all
    select '1,2' union all
    select '1,22,3,33'
    --------------开始查询--------------------------
    select len(col)-len(replace(col,',','')) from [tb]
    ----------------结果----------------------------
    /* -----------
    0
    1
    3(3 行受影响
    */
      

  5.   

    select 
    len(col)-len(replace(col,',',''))
    from tb
      

  6.   

    select len(Col1)-len(replace([Col1],',','')) from tablename