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
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
呵呵 这跟top 1 差不多吧
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
select * from @t where len(replace(a,' ','a'))=len(replace('f ',' ','a'))改红色部分即可
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 a where len(replace(a,' ',','))=(select min(len( replace(a,' ',','))) from @t)
select 1 where 'f'='f '总是成立的,比较的时候忽略后面的空格。 看来只能通过长度来判断了
insert @t
select 'f ' union all
select 'f ' union all
select 'f set rowcount 1
select * from @t where a='f'
set rowcount 0
insert @t
select 'f ' union all
select 'f ' union all
select 'f
'
select * from @t where a='f' and len(replace(a,' ','a'))=6
insert @t
select 'f ' union all
select 'f ' union all
select 'f '
select * from @t where a='f' and datalength(a)=6
例如:
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
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)
select 1 where 'f'='f '总是成立的,比较的时候忽略后面的空格。
看来只能通过长度来判断了
varchar字段未自动除去空格,可用中文空格代替