Sql LIKE statement. End Of String

2019-07-01 21:34发布

问题:

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.

回答1:

You could use

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


回答2:

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.



回答3:

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


回答4:

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 )


标签: tsql