xxx表中id字段是int型,有10条记录(id=11~20),执行select substring(str(id),1,1) from xxx 之后为何返回10行空白?

解决方案 »

  1.   

    declare @t table (id int)
    insert @t select 11
    insert @t select 12
    insert @t select 13
    insert @t select 14
    insert @t select 15
    insert @t select 16
    insert @t select 17
    insert @t select 18
    insert @t select 19
    insert @t select 20
    select substring(cast(id as varchar(2)),1,1) from @t
      

  2.   

    create table t(id int)
    insert into t(id)
    select 11 union 
    select 12 union
    select 13 union
    select 14 union
    select 15 union
    select 16 union
    select 17 union
    select 18 union
    select 19 union
    select 20 select substring(ltrim(str(id)),1,1) from t drop table t
      

  3.   

    用str(id)会在左边产生空格,所以都是空的
      

  4.   

    或者你用
    select substring(str(id),1,9) from xxx
    str() 总长度默认值为 10