How to Deal With Apostrophes in SQL Strings

Dealing with apostrophes in SQL strings can become a problem in your SQL database if you don't handle them correctly. Apostrophes are special characters used to define the beginning and the end of a string to be inserted into a SQL database. In SQL, you need to add an additional apostrophe in cases where a string contains apostrophes in its middle. The data in your database can be incorrect if you forget to use this simple rule.

Things You'll Need

  • Microsoft SQL Server Management Studio
Show More

Instructions

    • 1

      Launch Microsoft SQL Server Management Studio, expand your server from the Object Explorer pane and expand the "Databases" folder to view all your databases.

    • 2

      Click "New Query" to start a new query. Click the "Available Databases" combo box located next to the "Execute" icon on the toolbar. Choose the database you want to use from the combo box next to "Execute."

    • 3

      Copy and paste the following code to create a new table in your database:

      CREATE TABLE [dbo].[myTable]

      (

      [pID] [int] IDENTITY (1,1) NOT NULL,

      [dataColumn] NVARCHAR(100) NOT NULL,

      CONSTRAINT [PK_pID] PRIMARY KEY ([pID])

      )

      GO

    • 4

      Copy and paste the following code to insert words to the table using an apostrophe:

      INSERT INTO [dbo].[myTable]("dataColumn")

      VALUES ('word''with apostrophe')

    • 5

      Query your table to see the words added in the previous step with an apostrophe:

      SELECT * FROM [dbo].[myTable]

    • 6

      Press "F5" to execute the SQL code and view the query results.

Related Searches:

References

Comments

Related Ads

Featured