表 table1
列name              列id
'abcdefghijk'        1如何在查询列name时,只获取id为1 name值只截取前4个字符,即结果应为:
'abcd'
该怎么做?

解决方案 »

  1.   

    select id,name=left(ltrim(name),4) from table1 where id=1
      

  2.   

    use test
    go
    if object_id('test.dbo.table1') is not null drop table table1
    -- 创建数据表
    create table table1
    (
    id int,
    name char(30)
    )
    go
    --插入测试数据
    insert into table1 select 1,'abcdefghijk'
    union all select 4,'ghijkabcdef'
    go
    --代码实现
    select id,name=left(ltrim(name),4) from table1 where id=1/*
    id  name
    -------------
    1 abcd
    */
      

  3.   


    恩是的!加上ltrim()也只是防止出现' abcdefg'取值为' abc'的情况...