0

How can i get the records that contain a single quotation mark in SQL Server?

The following as you guess, is not valid, but you get the point

SELECT [Id], [Name] FROM [People] WHERE Name LIKE '%'%'
OrElse
  • 9,709
  • 39
  • 140
  • 253

4 Answers4

1

The double single quote is interpreted as a single quote in a string:

SELECT [Id], [Name] FROM [People] WHERE Name LIKE '%''%'
Gordon Linoff
  • 1,242,037
  • 58
  • 646
  • 786
0

You need to escape the quote

SELECT [Id], [Name] FROM [People] WHERE Name LIKE '%''%'
Valerica
  • 1,618
  • 1
  • 13
  • 20
0

In your query, the % are under 3 ' (like '%'%'). They should be under 4.

SELECT [Id], [Name] FROM [People] WHERE Name LIKE '%''%'

The % are now escaped in this query.

0

You can do the escape code as others have mentioned or you can use the char code 39, where CHAR(39) is '.

SELECT [Id], [Name] FROM [People] WHERE Name LIKE '%' + CHAR(39) + '%'
Zorkolot
  • 1,899
  • 1
  • 11
  • 8