0

I want to build a query like this:

SELECT * FROM T1 WHERE Charindex(ANY(SELECT City FROM Cities),T1ADDRESS)>0

As i understand, the ANY operator cannot stay as SQL functions argument. So, what is the alternative?

Suppose I want to write a UDF that returns 1 or 0 dependent on one input argument Address. How do I do this without for loop operator and without accessing to SELECT City FROM Cities array by index, as it can be done easily in the procedural languages?

4
  • You should not use functions on columns in the WHERE clause. Commented Sep 11, 2012 at 15:20
  • @LukasEder T-SQL for SQL Server Commented Sep 11, 2012 at 15:20
  • I think you are probably looking for the LIKE operator. Commented Sep 11, 2012 at 15:22
  • Note that both the LIKE operator and UDFs aren't great for performance. Would be useful if the city was normalised out of the address. Commented Sep 11, 2012 at 15:31

3 Answers 3

1

Have you tried a JOIN?

SELECT * 
FROM T1
INNER JOIN CITIES
    ON T1ADDRESS LIKE '%' + City + '%'

Not sure about performance however...

Sign up to request clarification or add additional context in comments.

Comments

0

How about,

SELECT 
              * 
    FROM 
            T1 
    WHERE
        EXISTS
        (
            SELECT City FROM Cities WHERE T1.T1ADDRESS LIKE '%' + City + '%' 
        )

Comments

0

You might want to do something like this?

SELECT * FROM T1 WHERE Charindex in (SELECT '%'+City+'%' FROM Cities)

which address the ANY problem but not everything you asked for.

2 Comments

Not, the IN operator finds exact matchings, but I want to find a substring matching
updated for substring problem. It is part of the solution though, I did not get your whole problem.

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.