人的特长,有三个选项:游泳、绘画、书法等。如果他会“游泳”,那么值为“01”,如果会“游泳、书法”,那么值为“01,03”
现在如果我想把它拆分成“游泳、绘画、书法”三个字段,每个字段根据原始字段的内容判断是/否那么我该怎么样写语句?是否先要把全部信息都处理一下,才能够进行拆分?小弟对SQL语句不太熟悉,请各位大大帮忙!

解决方案 »

  1.   

    人员姓名    人员编号 爱好(01游泳,02绘画,03书法)
    刘长艳 R200903000014 01,
    袁文珍 R200903000015 03,
    黄珍 R200903000019 01,02,
    王明俊 R200903000020 01,
    罗令军 R200903000021 02,03,01,
    曾军富 R200903000022 01,我想在右侧再新建3个列,分别为游泳,绘画,书法,然后里面的值从“爱好”这列来取。谢谢!
      

  2.   

    如果只有这三个值的话,就用下面这三句简单sql就可以实现了
    update tab1 set 游泳='是' where 爱好 like '%01%';
    update tab1 set 绘画='是' where 爱好 like '%02%';
    update tab1 set 书法='是' where 爱好 like '%03%';
      

  3.   

    哦还有为空的没考虑进去哟
    执行完上面的在把下面的执行下
    update tab1 set 游泳='否' where 游泳 is null; 
    update tab1 set 绘画='否' where 绘画 is null; 
    update tab1 set 书法='否' where 书法 is null;
      

  4.   


    create table new_table as
    select 姓名,编号,case when 爱好 like '%01%' then '是' else '否' end 游泳,
      case when 爱好 like '%02%' then '是' else '否' end 绘画,
      case when 爱好 like '%03%' then '是' else '否' end 书法
    from old_table
      

  5.   

    create table t_interest
    (
    name varchar2(10),
    code char(20),
    interest varchar2(20),
    swim char(1),
    draw char(1),
    writeing char(1)
    );insert into t_interest values('刘长艳','R200903000014','01,','','','');
    insert into t_interest values('袁文珍','R200903000015','03,','','',''); 
    insert into t_interest values('黄珍','R200903000019','01,02,','','','');
    insert into t_interest values('王明俊','R200903000020','01,','','',''); 
    insert into t_interest values('罗令军','R200903000021','02,03,01,','','','');
    insert into t_interest values('曾军富','R200903000022','01,','','',''); update t_interest set 
    swim = (case when (instr(interest,'01',1,1) = 0) then 'N' else 'Y' end),
    draw = (case when (instr(interest,'02',1,1) = 0) then 'N' else 'Y' end),
    writeing = (case when (instr(interest,'03',1,1) = 0) then 'N' else 'Y' end)
    commit;
    select * from t_interest;
      

  6.   

    UP
    经测试通过SQL> select * from person;
     
    NAME                 NO                   HABIT
    -------------------- -------------------- --------------------
    刘长艳               R200903000014        01,
    袁文珍               R200903000015        03
    黄珍                 R200903000019        01,02,
    王明俊               R200903000020        01,
    罗令军               R200903000021        02,03,01,
    曾军富               R200903000022        01,
     
    6 rows selected
     
    SQL> 
    SQL> select name,no,
      2    case when habit like '%01%' then '是' else '否' end swim,
      3    case when habit like '%02%' then '是' else '否' end printing,
      4    case when habit like '%03%' then '是' else '否' end writting
      5  from person;
     
    NAME                 NO                   SWIM PRINTING WRITTING
    -------------------- -------------------- ---- -------- --------
    刘长艳               R200903000014        是   否       否
    袁文珍               R200903000015        否   否       是
    黄珍                 R200903000019        是   是       否
    王明俊               R200903000020        是   否       否
    罗令军               R200903000021        是   是       是
    曾军富               R200903000022        是   否       否
      

  7.   


    好像以前suncrafted兄弟,有帮人解决过这样的case的吧