简体   繁体   中英

SQL Server stored procedure to search list of values without special characters

What is the most efficient way to search a column and return all matching values while ignoring special characters?

For example if a table has a part_number column with the following values '10-01' '14-02-65' '345-23423' and the user searches for '10_01' and 140265 it should return '10-01' and '14-02-65' .

Processing the input to with a regex to remove those characters is possible, so the stored procedure could could be passed a parameter '1001 140265' then it could split that input to form a SQL statement like

SELECT *
FROM MyTable
WHERE part_number IN ('1001', '140265')

The problem here is that this will not match anything. In this case the following would work

SELECT *
FROM MyTable
WHERE REPLACE(part_number,'-','') IN ('1001', '140265')

But I need to remove all special characters. Or at the very least all of these characters ~!@#$%^&*()_+?/\\{}[]; with a replace for each of those characters the query takes several minutes when the number of parts in the IN clause is less than 200.

Performance is improved by creating a function that does the replaces, so the query takes less than a minute. But without removals the query takes around 1 second, is there any way to create some kind of functional index that will work on multiple SQL Server engines?

You could use a computed column and index it:

CREATE TABLE MyTable (
    part_number VARCHAR(10) NOT NULL,
    part_number_int AS CAST(replace(part_number, '-', '') AS int)
    );
ALTER TABLE dbo.MyTable ADD PRIMARY KEY (part_number);
ALTER TABLE dbo.MyTable ADD UNIQUE (part_number_int);

INSERT INTO dbo.MyTable (part_number)
VALUES ('100-1'), ('140265');

SELECT *
FROM dbo.MyTable AS MT
WHERE MT.part_number_int IN ('1001', '140265');

Of course your replace statement will be more complex and you'll have to sanitize user input the same way you sanitize column values. But this is going to be the most efficient way to do it.

This query can now seek your column efficiently: 在此输入图像描述

But to be honest, I'd just create a separate column to store cleansed values for querying purpose and keep the actual values for display. You'll have to take care of extra update/insert clauses, but that's a minimum damage.

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