2012-09-18 90 views
3
create table #temp(name nvarchar(10)) 
insert into #temp values('one') 
select * from #temp where name = 'one' 
select * from #temp where name = 'one ' --one with space at end 
drop table #temp 

在上面我已经使用了nvarchar作为名称。 我的要求是结果应该存在的第一个选择查询,它不应该返回第二个查询。不要修剪名字。建议我可以在sql server中使用哪种数据类型?在sql server中等效的Oracle varchar2

回答

1

它不是可以解决此问题的数据类型。你需要看看这篇文章: INF: How SQL Server Compares Strings with Trailing Spaces

的SQL Server遵循ANSI/ISO SQL-92规范对如何比较字符串 与空间(第8.2节 ,一般规则#3)。 ANSI标准要求填充字符 用于比较的字符串,以便它们的长度在比较之前匹配 。填充直接影响WHERE 和HAVING子句谓词以及其他Transact-SQL字符串 比较的语义。例如,对于大多数比较操作,Transact-SQL将字符串'abc'和 'abc'视为等效。

有几种方法可以解决这个问题,一种是使用Like

select * from #temp where name like 'one ' --one with space at end 

这将不会返回任何结果。

您应该看到这篇文章:Testing strings for equality counting trailing spaces AnthonyBloesch

相关问题