0

I am trying to write a query to find a string withing a string. In Oracle I used regexp_like but I don't see such function in SQL Server 2016. I have a table that has an address column such as:

ADDRESS
--------
345 E 149 ST NY NY
345 EAST 149 STREET NY NY
444 CHEST AVE NY NY
444 CHEST AVENUE NY NY

I want to write a query that search for 345 [E OR East] follow by 149. in the case of 444 chest, I want a query to search for 444 chest[ ave or avenue].

I have something like the following which doesn't work

select * from table1 where address like '345[EEAST] 149%'

Basically I want to tell the query to get any address that start with 345 E or 345 EAST follow by 149.

Can someone help me write a query for this? I know I can use OR clause with two different address but if I have multiple addresses with different pattern then OR clause will not be efficient method. I'm looking into using some type of regular expression.

1
  • Have a look at LIKE but you need to add the wildcard % for it to be any use. Otherwise try charindex or patindex. Thats what is on offer with SQL Server. Commented May 13, 2020 at 2:32

3 Answers 3

1

Unfortunately the amount of support for LIKE expression is very limited currently in SQL Server. Your requirement can be satisfied by below queries

DECLARE @table table(address VARCHAR(1000))

INSERT INTO @table
values
('345 E 149 ST NY NY')
,('345 EAST 149 STREET NY NY')
,('444 CHEST AVE NY NY')
,('444 CHEST AVENUE NY NY')


SELECT * FROM @table WHERE Address LIKE '345 E%149 ST% NY NY'

SELECT * FROM @table WHERE Address LIKE '444 CHEST AVE% NY NY'

Result Set

+---------------------------+
|          address          |
+---------------------------+
| 345 E 149 ST NY NY        |
| 345 EAST 149 STREET NY NY |
+---------------------------+


+------------------------+
|        address         |
+------------------------+
| 444 CHEST AVE NY NY    |
| 444 CHEST AVENUE NY NY |
+------------------------+
Sign up to request clarification or add additional context in comments.

Comments

0

Use the LIKE operator, with separate conditions for each match:

SELECT *
FROM table1
WHERE ADDRESS LIKE '345 E 149 %' OR ADDRESS LIKE '345 EAST 149 %';

3 Comments

hi Tim, thanks for your reply. I am aware that i can use OR but as stated in my original post i can have different pattern of the same address. if i use OR then i have to use an OR for each different pattern of the same address. im looking for regular expression without using OR
@yoohoo did you try LIKE '345 % 149 %'?
Unfortunately, SQL Server has no native regex support.
0

You can try this in WHERE Clause.

SELECT *
FROM   @T
WHERE  (
           1 = CASE 
                    WHEN ADDRESS LIKE '%345_E_149%' THEN 1
                    WHEN ADDRESS LIKE '%345_EAST_149%' THEN 1
                    WHEN ADDRESS LIKE '%444_CHEST_AVE%' THEN 1
                    WHEN ADDRESS LIKE '%444_CHEST_AVENUE%' THEN 1
               END
       )

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.