Reputation: 57
I need to store variable string length in a SQL Server table, length can vary 0 to 10000 or so characters.
I need to filter the text with like operator %abc
or abc%
.
I thought of using varchar
or nvarchar
, but I'm unable to create index on this datatype.
Please suggest design advise for choosing the column data type and make the column indexable.
Thank you
Upvotes: 0
Views: 2801
Reputation: 754538
I thought of using varchar or nvarchar, but I'm unable to create index on this datatype.
Those are the proper datatypes for text based information.
The reason you're not able to create an index has nothing to do with the datatype per se - but in SQL Server, an index can be created only if the column (or set of columns) in the index have a max. theoretical size of 900 bytes (or less).
This means, a VARCHAR(1000)
(or VARCHAR(MAX)
) cannot be indexed. This size limit is your issue - not the datatype.
Solution: there's really only one solution: use fewer bytes in the columns you want to index. Or alternatively, as Gordon suggested, check out the full-text indexing capabilities of SQL Server.
Upvotes: 3
Reputation: 1269883
SQL Server will use an index for like 'abc%'
. However, it will not use the index if the wildcard is first.
If you are searching for complete words, then you should investigate contains()
and full text search.
Upvotes: 3