带空格的字符串比较?

lidanzi 2009-11-06 05:55:18

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
...全文
327 19 打赏 收藏 转发到动态 举报
写回复
用AI写文章
19 条回复
切换为时间正序
请发表友善的回复…
发表回复
Guardians 2009-11-09
  • 打赏
  • 举报
回复
select
chkaka 2009-11-08
  • 打赏
  • 举报
回复
这个主要是空格的问题,
daiyueqiang2045 2009-11-07
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 fredrickhu 的回复:]
SQL codeDataLength()函数返回的是字符串字节的长度,包含后缀空格。而Len()函数返回的是字符串的字符长度,不包含后缀的空格。
例如:SELECTLEN('string'),LEN('string'),DATALENGTH('string'),DATALENGTH('string'),LEN(N'string'),LEN(N'string'),DATALENGTH(N'strin?-
[/Quote]

学习。。。
feixianxxx 2009-11-07
  • 打赏
  • 举报
回复
。。。。
cxmcxm 2009-11-07
  • 打赏
  • 举报
回复
楼主是否要区分有无带空格.
varchar字段未自动除去空格,可用中文空格代替
devilidea 2009-11-07
  • 打赏
  • 举报
回复
路过
lidanzi 2009-11-06
  • 打赏
  • 举报
回复

select 1 where 'f'='f '

总是成立的,比较的时候忽略后面的空格。
看来只能通过长度来判断了
ChinaJiaBing 2009-11-06
  • 打赏
  • 举报
回复


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)


Rotel-刘志东 2009-11-06
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 xiequan2 的回复:]
SQL codeDECLARE@ttable(avarchar(20))insert@tselect'f'unionallselect'f'unionallselect'f'select*from@twhere a='f'anddatalength(a)=6
[/Quote]
这样写法应该正确的。
--小F-- 2009-11-06
  • 打赏
  • 举报
回复
所以7楼的正解
--小F-- 2009-11-06
  • 打赏
  • 举报
回复
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



dawugui 2009-11-06
  • 打赏
  • 举报
回复
你定义的是varchar(20)型数据,你F后面的那些空格不管用.
xiequan2 2009-11-06
  • 打赏
  • 举报
回复
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
dtxh168 2009-11-06
  • 打赏
  • 举报
回复
select * from @t where len(replace(a,' ','a'))=len(replace('f ',' ','a'))

改红色部分即可
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 lidanzi 的回复:]
如果是'f '就是第二行。

[/Quote]
先把要求说清楚吧
lidanzi 2009-11-06
  • 打赏
  • 举报
回复
如果是'f '就是第二行。
dtxh168 2009-11-06
  • 打赏
  • 举报
回复
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
lidanzi 2009-11-06
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 libin_ftsafe 的回复:]
SQL codeDECLARE@ttable(avarchar(20))insert@tselect'f'unionallselect'f'unionallselect'f

set rowcount 1
select * from @t where a='f'
set rowcount 0
[/Quote]
呵呵 这跟top 1 差不多吧
子陌红尘 2009-11-06
  • 打赏
  • 举报
回复
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

34,588

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧