DECLARE @Table
TABLE (
id INTEGER IDENTITY PRIMARY KEY,
data VARCHAR(200) NOT NULL
);
INSERT @Table (data)
SELECT 'here is my phone number 12345678. but my cell is 44444444.' UNION ALL
SELECT '987651234 is my balance' UNION ALL
SELECT 'i like the number 4444444. it is cool' UNION ALL
SELECT '66666666';
SELECT id,
pos =
(
-- Find the lowest of the positions found
SELECT MIN(Positions.pos)
FROM (
-- Try to find both patterns
SELECT pos = CHARINDEX('4444', T.data)
UNION ALL
SELECT pos = CHARINDEX('1234', T.data)
) Positions
-- Exclude cases where no match ws found
WHERE Positions.pos > 0
)
FROM @Table T;
I changed the sample data a little to be more useful :doze:
Paul White
SQLPerformance.com
SQLkiwi blog
@SQL_Kiwi