You can create a computed column and then create an index on it.
Note: SQL Server Index key columns have a 900 byte size limit. Since your column is NVARCHAR
, it consumes 2 bytes for each character. So, let's can cap the index at 400 characters (800 bytes).
To conserve space, we can further restrict this column to contain a value, only if it meets your required conditions.
ALTER TABLE MyTable
ADD A_NoSpace AS (CASE WHEN REPLACE(A, ' ', '') IN ('aasa', 'asa', 'wew','wewe') THEN LEFT(REPLACE(A, ' ', ''), 400) END) PERSISTED
CREATE NONCLUSTERED INDEX IX_A_NoSpace ON MyTable (A_NoSpace)
Being a PERSISTED
column, calculation will take place only during INSERT
or UPDATE
operations.
You can now use this column in your query:
SELECT *
FROM
MyTable
-- Don't need to check if value is in the list,
-- because we're already doing this in the computed column definition
WHERE A_NoSpace IS NOT NULL