DECLARE @t table(a varchar(20))
insert @t
select 'f     ' union all
select 'f  ' union all
select 'f            '
select * from @t where a='f'
 上面方法得到的是所有数据,怎么只取第一行数据?不知道怎么弄  除了select top 1

解决方案 »

  1.   

    DECLARE @t table(a varchar(20))
    insert @t
    select 'f     ' union all
    select 'f  ' union all
    select 'f      set rowcount 1
    select * from @t where a='f'
    set rowcount 0
      

  2.   

    呵呵 这跟top 1 差不多吧
      

  3.   

    DECLARE @t table(a varchar(20))
    insert @t
    select 'f     ' union all
    select 'f  ' union all
    select 'f     
           '
    select * from @t where a='f' and len(replace(a,' ','a'))=6
      

  4.   

    select * from @t where len(replace(a,' ','a'))=len(replace('f  ',' ','a'))改红色部分即可
      

  5.   

    DECLARE @t table(a varchar(20))
    insert @t
    select 'f     ' union all
    select 'f  ' union all
    select 'f            '
    select *  from @t where a='f' and datalength(a)=6
     
      

  6.   

    你定义的是varchar(20)型数据,你F后面的那些空格不管用.
      

  7.   

    DataLength()函数返回的是字符串字节的长度,包含后缀空格。而Len()函数返回的是字符串的字符长度,不包含后缀的空格。
    例如:
    SELECT
      LEN('string'),
      LEN('string   '),
      DATALENGTH('string'),
      DATALENGTH('string   '),
      LEN(N'string'),
      LEN(N'string   '),
      DATALENGTH(N'string'),
      DATALENGTH(N'string   ')返回值是 6, 6, 6, 9, 6, 6, 12, 18强调:Len()是忽略后缀空格,前缀空格或字符中间空格是包含的。如:select len(' str'),          len(' str str ')返回值是4, 8
      

  8.   

    DECLARE @t table(a varchar(20))
    insert @t
    select 'f     ' union all
    select 'f  ' union all
    select 'f            '
    select * from @t a where len(replace(a,' ',','))=(select min(len( replace(a,' ',','))) from @t) 
      

  9.   


    select 1 where 'f'='f   '总是成立的,比较的时候忽略后面的空格。
    看来只能通过长度来判断了
      

  10.   

    楼主是否要区分有无带空格.
    varchar字段未自动除去空格,可用中文空格代替