8bitcat
8bitcat

Reputation: 2234

T-sql compare two variables see if the other one holds the first

I have seen this question but not asked like this, if there is a question like please link it.

I'm using T-SQL and I'm writing a stored procedure.

So I have a @masterstring let's say

'www.apple.com'

and a @slavestring

'www.apple.com/subdirectory'

I use the code below to see if I should add rows to the database.

What I want to do is to add rows to the database if it passes this clause.

IF NOT EXISTS (SELECT ranking_date, ranking_keyword, ranking_id_doman
                FROM [dbo].[t_ranking] 
                WHERE ranking_id_doman = @domanID
                AND ranking_keyword = @keyword
                AND ranking_searchesincluded = @searchesincluded) 
                AND @masterstring like @slavestring +'%'

@masterstring stays the same through the whole process, but @slavestring changes for every insert I do.

QUESTION
How do I write the AND statement so that every @slavestring that contains the @masterstring will be true?

I have tried with

Attempt #1:

AND @masterstring like '%' + @slavestring + '%'

Attempt #2:

@masterstring like @slavestring + '%'

None of them work...

Upvotes: 2

Views: 1465

Answers (1)

Evgeny Lukashevich
Evgeny Lukashevich

Reputation: 1517

You can use SUBSTRING and LEN functions to do this:

IF NOT EXISTS (SELECT ranking_date, ranking_keyword, ranking_id_doman
               FROM [dbo].[t_ranking] 
               WHERE ranking_id_doman = @domanID
               AND ranking_keyword = @keyword
               AND ranking_searchesincluded = @searchesincluded) 
               AND SUBSTRING(@slavestring, 1, LEN(@masterstring)) = @masterstring

Upvotes: 1

Related Questions