有表如下,初始时各列都是设成字符类型
Factor 张三 李四 王五
Math 90 80 95
Class A1 A1 A2
History 90 89 85现在想增加一列用于显示第二列减第一列的结果,字符相减,结果默认为0,怎么实现???

解决方案 »

  1.   

    Factor 张三 李四 王五
    Math  90  80  95
    Class  A1  A1  A2
    History 90  89  85要变成:
    Factor 张三 李四 王五 李四一张三
    Math  90  80  95  -10
    Class  A1  A1  A2  0
    History  90  89  85  -1
      

  2.   

    select *,case when isnumeric(李四)=1 and isnumeric(张三)=1 then 李四-张三 else 0 end from tb
      

  3.   

    alter table biao add ziduan int default('0')这个是用来新增列的
      

  4.   

    select * ,李四-张三 from biao
    alter table biao add ziduan int default('0')
    是不是这样啊 
      

  5.   


    create table #test(Factor varchar(20), 张三 varchar(20), 李四 varchar(20), 王五 varchar(20))insert #test
    select 'Math', '90', '80', '95'
    insert #test
    select 'Class', 'A1' ,'A1' ,'A2'
    insert #test
    select 'History', '90', '89', '85'
    alter table #test
    add iii as
    case when isnumeric(李四)=1 and isnumeric(张三)=1 then ltrim(cast(李四 as int)-cast(张三 as int)) else '0' endselect *  from #test
    Factor               张三                   李四                   王五                   iii
    -------------------- -------------------- -------------------- -------------------- ------------
    Math                 90                   80                   95                   -10
    Class                A1                   A1                   A2                   0
    History              90                   89                   85                   -1(3 行受影响)