(Этот ответ предполагает, что вы используете SQL Server)
Я думаю, что вы можете решить проблему, заменив одну -
за раз, и посмотрите, сможете ли вы найти точное соответствие для ключевого слова:
DECLARE @t TABLE (id int, keyword VARCHAR(8000));
INSERT INTO @t VALUES
(1, 'usedredcars'),
(2, 'used-redcars'),
(3, 'usedred-cars'),
(4, 'used-red-cars'),
(5, 'florida-usedredcars'),
(6, 'usedredcars-florida'),
(7, 'florida-usedredcars-florida'),
(8, 'rarelyused-red-cars'),
(9, 'used-red-carshops');
WITH cte AS (
-- pad the seed string with hyphens to simplify edge case handling
SELECT id, keyword, '-' + keyword + '-' AS search
FROM @t AS base
UNION ALL
-- replace hyphens from the middle one at a time
SELECT id, keyword, STUFF(search, CHARINDEX('-', search, 2), 1, '')
FROM cte AS prev
WHERE NULLIF(CHARINDEX('-', search, 2), LEN(search)) > 0
-- BE ADVISED that STUFF can return unexpected size varchar and thus:
-- Types don't match between the anchor and the recursive part
-- explicitly cast the result to desired size if that happens
)
SELECT *
FROM cte
WHERE search LIKE '%-usedredcars-%'
DB Fiddle