How to Insert Unicode Text to SQL Server from Query Window

how to insert unicode text to SQL Server from query window

The following should work, N indicates a "Unicode constant string" in MSSQL:

INSERT INTO tForeignLanguage ([Name]) VALUES (N'Араб')

SQL Server 2012 not showing unicode character in results

Per the comment discussion on your question, your SQL code is correct and the unicode character is being updated, but SQL Server Management Studio cannot (for some reason) display this unicode character in the GRID resultset.
If you change your result view to TEXT (control+T), you should see the unicode character.

"If you use SSMS for your queries, change to output type from "Grid" to "Text", because depending on the font the grid can't show unicode."

  • sql server 2008 not showing and inserting unicode characters

(ASP.NET MVC4 C#) Insert UNICODE to SQL Server

Any data access code should never be writing:

 INSERT INTO [USER] ("username", "fullname") 
VALUES ('admin', N'á')

because unicode aside, this is simply a SQL injection risk. Your data access should actually be doing something like

 INSERT INTO [USER] ("username", "fullname") 
VALUES (@username, @fullname)

adding parameters with those values. You don't make it clear what data access technology you are using, but it looks like either EF or LINQ-to-SQL, in which case the ORM will get this right. They all know all about unicode, and besides: the default (when adding a parameter with a string value) is to pass it over as unicode. You need to actually tell it to not do unicode. So I have to conclude that one of:

  • the database column is varchar instead of nvarchar
  • the database column was varchar when you created the ORM model, and you haven't updated the model to tell it that it is now nvarchar - so the model is working with what it knows, and adding the parameter as ANSI rather than unicode

Either fix the database column, or update the model to tell it that the column is unicode.

How to insert unicode (arabic) characters into SQL Server database

You should always use parametrized queries to avoid SQL injection attacks. Parameters also give you the ability to explicitly define what data types and which length you want. Furthermore, by using parameters, you don't need to fiddle with lots of single and double quotes and so forth - the code becomes much cleaner and easier to read - and you avoid a lot of errors, too!

Try code something like this:

// define your INSERT statement with PARAMETERS
string insertStmt = "INSERT INTO dbo.Table1(title, datee, post, cat, imageurl) " +
"VALUES(@title, @datee, @post, @cat, @imageurl)";

// define connection and command
using(SqlConnection conn = new SqlConnection(yourConnectionStringHere))
using (SqlCommand cmd = new SqlCommand(insertStmt, conn))
{
// define parameters and set their values
cmd.Parameters.Add("@title", SqlDbType.NVarChar, 100).Value = TextBox1.Text.Trim();
cmd.Parameters.Add("@datee", SqlDbType.DateTime).Value = DateTime.Now;
cmd.Parameters.Add("@post", SqlDbType.NVarChar, 100).Value = TextBox2.Text.Trim();
cmd.Parameters.Add("@cat", SqlDbType.NVarChar, 100).Value = DropDownList1.SelectedItem.Text.Trim();
cmd.Parameters.Add("@imageurl", SqlDbType.NVarChar, 250).Value = path;

// open connection, execute query, close connection
conn.Open();
int rowsInserted = cmd.ExecuteNonQuery();
conn.Close();
}

How to use 'LIKE' statement with unicode strings?

Make sure the collation on your table supports unicode.

using unicode text in sql server 2008

Try

declare @unicodetext nvarchar(250)
set @unicodetext = N'बन्द'
select @unicodetext

Maybe you noticed that SQL Server uses N to any quoted string input when generating DDL scripts for you.



Related Topics



Leave a reply



Submit