数据源     →   结果
11-2-8     →   11-2
11-2-10  →   11-2
11-2-11  →   11-2
11-9   →   11-9
11-15   →      11-15
10        →      10
11-6      →      11-6
2     →   2
目标需求
采用SQL方法对有两个"-"为一个数值.
如11-2-8     →   11-2
select distinct 字段分离 from table
结果为
11-2
11-9
11-15
10
11-6
2
也就是数据源为8条记录,经过select distinct 处理后为6条记录.
谢谢,各位大侠.

解决方案 »

  1.   

    select Distinct substring(fName,1,4) from tName
      

  2.   

    我感觉用SQL不可解,要用字典方法.求出唯一性.
      

  3.   

    我也觉得SQL不可解,建议采用程序解决
      

  4.   

    一个SQL不好写
    存储过程还好~
      

  5.   

    try:select distinct
           case when charindex('-',pid)>0 
                then (case when charindex('-',pid,charindex('-',pid)+1)>0 
                           then substring(pid,1,charindex('-',pid,charindex('-',pid)+1)-1) 
                           else substring(pid,1,charindex('-',pid)-1) 
                      end) 
                else pid 
            end
    from tb
      

  6.   


    谢谢回复,用的是EXCEL-VBA,在SQL中没有CASE功能.