how to insert unicode text to SQL Server from query window

Posted on

how to insert unicode text to SQL Server from query window – This article will take you through the common SQL errors that you might encounter while working with sql, unicode,  . The wrong arrangement of keywords will certainly cause an error, but wrongly arranged commands may also be an issue. SQL keyword errors occur when one of the words that the SQL query language reserves for its commands and clauses is misspelled. If the user wants to resolve all these reported errors, without finding the original one, what started as a simple typo, becomes a much bigger problem.

SQL Problem :

I’m using the following code:

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

this value inserted like this ‘????’

How do I insert unicode text from the sql management studio query window?

Solution :

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

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

The perfect solution with data type limitation:

Basically in MS-SQL Server Amharic text not working properly when the column datatype is ‘text’.Therefore to put Amharic text on column with datatype text, first change the text datatype to ‘nvarchar(MAX)‘ or just ‘nvarchar‘ with any char length that MS-SQL Server supported.

In my case, the task at hand was to update an SQL table which contains list of countries in two languages in which the local language (Amharic) column was null so executing the following works fine.

    Update [tableName] set [columnName] = N'አሜሪካ'

The N in N’አሜሪካ’ is the key to put the string as it is in your specific column.

Thanks to Ian’s answer, you can directly run this code from query window:

declare @FamilyName nvarchar(40)
set @FamilyName = N'嗄嗄嗄'

insert into table(LoginName, Password)  select @FamilyName as LoginName, 123 as Password

However, if you wish to perform the above insert through stored procedure, it’s needed to attach N as prefix:

CREATE PROCEDURE Example
    @FAMILY_NAME   NVARCHAR(40)
AS
BEGIN

    SET NOCOUNT ON;
    declare @query nvarchar(400);


    set @query  ='insert into table(LoginName, Password)  select N'''+ @FAMILY_NAME +''' as LoginName, 123 as Password';

    EXECUTE sp_executesql @query;

END

Hope this helps..

Just make datatype NVarchar in database and following;

internal string InsertUpdate(classname obj)
{
    SqlParameter[] sqlparam = new SqlParameter[];
    sqlparam[] = new SqlParameter("@DESC1", SqlDbType.NVarChar);
    sqlparam[].Value = NullHandler.String(obj.DESC1);
    sqlparam[] = new SqlParameter("@DESC2", SqlDbType.NVarChar);
    sqlparam[].Value = NullHandler.String(obj.DESC2);
    obj.InsertUpdateTable("spname", "sp", sqlparam);
    if (sqlparam[].Value != DBNull.Value)
        return sqlparam[].Value.ToString();
}

Finding SQL syntax errors can be complicated, but there are some tips on how to make it a bit easier. Using the aforementioned Error List helps in a great way. It allows the user to check for errors while still writing the project, and avoid later searching through thousands lines of code.

Leave a Reply

Your email address will not be published. Required fields are marked *