加入我一个列中的数据时这样的
                pd_order_cd
 第一条记录     t1,t2,t3,t4
 第二条记录     t5
 第三条记录     t6
我现在要用SQL语句去算3条记录的总和 应该怎么做? 他的总和应该是6请大虾门看看  小弟在此谢谢了

解决方案 »

  1.   


    --> 数据库版本:
    --> Microsoft SQL Server 2008 (RTM) - 10.0.1600.22
    --> 测试数据:students
    IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'students') 
    AND type in (N'U')) 
    DROP TABLE students
    GO---->建表
    create table students([pd_order_cd] varchar(11))
    insert students
    select 't1,t2,t3,t4' union all
    select 't5' union all
    select 't6'
    GO--> 查询结果
    SELECT  sum(LEN([pd_order_cd])-LEN(replace([pd_order_cd],',',''))+1) FROM students
    --> 删除表格
    --DROP TABLE students
      

  2.   


    IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[pd_order_cd]') 
    AND type in (N'U')) 
    DROP TABLE [TB]
    GO
    --建表
    create table [pd_order_cd](content varchar(100))
    insert [pd_order_cd]
    select 't1,t2,t3,t4' UNION ALL
    select 't5' UNION ALL
    select 't6'
    GO
    select sum(len(replace(content,',',',,'))-len(content)+1) from pd_order_cd
      

  3.   


    declare @table table (pd_order_cd varchar(12))
    insert into @table
    select 't1,t2,t3,t4' union all
    select 't5' union all
    select 't6'select sum(len(pd_order_cd+',')-len(replace(pd_order_cd,',',''))) from @table
      

  4.   

     非常感谢wxf163  结贴