How to Search for Apostrophes in T-SQL
- 1). Click the Windows "Start" button and go to "All Programs." Go to the Microsoft SQL Server 2008 R2 folder, expand its contents and click "SQL Server Management Studio" to launch the development environment.
- 2). Click the "Connect" button and connect to the server hosting the database.
- 3). Expand the Databases folder within the Object Explorer window and right-click on the database to be queried. Select "New Query" to open a new T-SQL query window.
- 4). Use "CHAR(39)" combined with wildcard characters ("%") in the WHERE clause to query rows that contain apostrophes in a specific column. "CHAR(39)" in T-SQL represents character 39 in the ASCII character set, which is an apostrophe.
For example, to select rows from the Contacts table in the AdventureWorks database that have LastName columns containing apostrophes anywhere in the column data, use the following query:
SELECT *
FROM [AdventureWorks].[Person].[Contact]
WHERE LastName LIKE '%' + CHAR(39) + '%'