简体   繁体   中英

SQL Server column datatype and indexable

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

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 .

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM