I am using Microsoft SQL Server 2008 R2 (with the latest service pack / patch) and the database sort is SQL_Latin1_General_CP1_CI_AS.
The following code:
SET ANSI_PADDING ON; GO CREATE TABLE Test ( Code VARCHAR(16) NULL ); CREATE UNIQUE INDEX UniqueIndex ON Test(Code); INSERT INTO Test VALUES ('sample'); INSERT INTO Test VALUES ('sample '); SELECT '>' + Code + '<' FROM Test WHERE Code = 'sample '; GO
gives the following results:
(1 row (s) affected)
Msg 2601, Level 14, State 1, Line 8
Cannot insert duplicate key string in object 'dbo.Test' with unique index 'UniqueIndex'. The value of the duplicate key is (sample).
The statement is complete.
------------
> sample <
(1 row (s) affected)
My questions:
- I assume the index cannot store trailing spaces. Can someone point me to the official documentation that defines / defines this behavior?
- Is there a way to change this behavior, that is, make it recognize the "pattern" and "pattern" as two different values ββ(by the way, these are they), so both can be in the index.
- Why on earth does SELECT return a string? SQL Server should do something really funny / clever with spaces in the WHERE clause, because if I remove the uniqueness in the index, both INSERTs will work fine, and SELECT will return two rows!
Any help / pointer in the right direction would be greatly appreciated. Thanks.
sql-server tsql string-comparison unique-index
Eric
source share