Egor4eg
Egor4eg

Reputation: 2708

Sql LIKE statement. End Of String

I have to select all fields which contain a specific text and ended with ", ', ) or blank space characters or this text is placed in the end of string.

So, I need something like that:

select *
from MyTable
Where Column1 like '%' + @text + '["'') ]%'

This query works fine until text is not placed in the end of Column1.

Upvotes: 3

Views: 4681

Answers (4)

Skrol29
Skrol29

Reputation: 5597

This should work better and faster:

SELECT * FROM MyTable
WHERE
   ( (RIGHT(Column1,1) IN (' ', '"', '''', ')'))
     AND
     (SUBSTRING(Column1, LENGTH(@text)-1, LENGTH(@text))=@text)
   ) 
OR ( RIGHT(Column1, LENGTH(@text))=@text )

Upvotes: 0

Quassnoi
Quassnoi

Reputation: 425683

SELECT  *
FROM    MyTable
WHERE   Column1 LIKE '%' + @text + '%["'') ]'
        OR Column1 LIKE '%' + @text

Upvotes: 0

Tim
Tim

Reputation: 5421

Why not try OR?

 Where Column1 like '%' + @text + '["'') ]%'
  or
  Column1 like '%' + @text + '["'') ]'

Or do I misunderstand the requirement? Your question is a little hard to follow.

Upvotes: 0

Martin Smith
Martin Smith

Reputation: 453668

You could use

Where Column1 + '"' like '%' + @text + '["'') ]%'

Upvotes: 5

Related Questions