Tal Malaki
Tal Malaki

Reputation: 422

How to insert special characters to a table row via SQL Editor

I have a table in my SQL Server database with a column of type nvarchar(50);

I tried to do this:

  1. Right click on my table

  2. Selecting "Edit 200 first rows"

  3. Typing in that column 'a'

I get an error like this:

Error source: .Net SqlClient Data Provider.
Error message: Incorrect syntax near 'a'.
Correct the errors and retry".

Why do I get this error message and how can I fix this?

Upvotes: 0

Views: 3652

Answers (2)

Nam_Unity
Nam_Unity

Reputation: 1

I have had the same error.
I want to insert a value I am Nam 'ABC' into MyTable at field Description as following SQL statement

Insert Into table MyTable (Description) values('I am Nam 'ABC'');

Once Insert command executed the error occur immediately

Incorrect syntax near 'ABC'.

Note that the reason why is SQL will understand character " ' " which before ABC belongs to character " ' " before I, and one after ABC belongs to " ' " at the end. As a result ABC does not belong to SQL statement anymore therefore it makes a break of statement.

Here is my solution:
I added two single quotes for each side of ABC

Insert Into table MyTable (Description) values('I am Nam ' 'ABC' ' ');

Upvotes: 0

dev
dev

Reputation: 715

I have used aqua data studio and tested this it is allowing me.

my result:::

 id     testc     
 -----  --------- 
 1      'asdfasd'

If you want to use it thrugh .net then while passing the string add the escape sequences. suppose if you are sending "'"--single quote.. then send I have used and tested this it like "\'" is allowing me.

refer below link:::

http://www.codeproject.com/Questions/157918/How-can-I-insert-a-special-character-in-sql-server

Upvotes: 2

Related Questions