34,588
社区成员
发帖
与我相关
我的任务
分享
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 1 where 'f'='f '
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)
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
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
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
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