在数据库中有一张表 a 其中有两列 id (int) 和name (nchar) 
有四行数据如下 
         1 abc
         2 abcc
         3 abccc
         4 bcd
  select * from a where name like 'a%c'
查询出来时空的 请问这是为什么

解决方案 »

  1.   

    name nvarchar(100)  
      

  2.   

    用nvarchar,因为nchar是定长,不足位数的后面是空格,不是'c',所以查询结果为空name like 'a%c' 是查询以a开头,c结尾的数据
      

  3.   

    那要查 以a 开头 中间含有c 那like 该怎么写?
      

  4.   

    那要查询 以a开头中间还有c那该怎么写?
     like 'a%c '?
      

  5.   

    like '%a%c%'
    ---测试数据---
    if object_id('[tb]') is not null drop table [tb]
    go
    create table [tb]([id] int,[name] nchar(10))
    insert [tb]
    select 1,'abc' union all
    select 2,'abcc' union all
    select 3,'abccc' union all
    select 4,'bcd'
     
    ---查询---
    select * from tb where name like 'a%c%'---结果---
    id          name
    ----------- ----------
    1           abc       
    2           abcc      
    3           abccc     (3 行受影响)
      

  6.   

    select * from a where name like 'a%c%'
      

  7.   

    select * from a where name like 'a%c%'
      

  8.   

    select * from a where name like 'a%c%'