1

the UPDATE gives ???? if the updater field was written in Arabic and this is my query:

UPDATE  students 
SET first_name = 'الاسم' , last_name = 'الاسم الاخير' , 
    father_name = 'الاسم الاخير' , mother_name = '', 
    birth_date = '1/1/1990 12:00:00 AM' , education_level = '' , 
    address = '' , notes = '' 
WHERE student_id = 33

And here is the result of the update:

student_id  first_name  last_name   mother_name     father_name   birth_date    
33           ?????      ?????          ??????       ???????????   1990-01-01

//the answer is great and thank you people, another question is that I am using this UPDATE syntax in my C# program

command.CommandText = "UPDATE  students SET " +
        "first_name = " + "'" + first_name + "'" + " , last_name = " + "'" + last_name + "'" +
         " , father_name = " + "'" + father_name + "'" + " , mother_name = " + 
        "'" + mother_name + "'" + ", birth_date = " + "'" + birth_date + "'" +
        " , education_level = " + "'" + education_level + "'"  +
        " , address = " + "'" + address + "'" + " , notes = " + "'" + notes + "'" +
        " WHERE student_id = " + id ;

//how to use the character N

Yasser
  • 1,725
  • 4
  • 22
  • 28
  • There is a similiar question: http://stackoverflow.com/questions/2881682/how-to-insert-arabic-characters-into-sql-database – Chris Jun 18 '12 at 10:51

2 Answers2

9

You have forgotten the N prefix before your string literals which is required so they will be treated as nvarchar rather than varchar

SET first_name = N'الاسم' etc.

without that the text is coerced into whatever characters the code page of your default collation can deal with.

Martin Smith
  • 438,706
  • 87
  • 741
  • 845
  • that's AWESOME, but how about writing the UPDATE into C# – Yasser Jun 18 '12 at 10:55
  • Might want to re-word that first sentence. At the moment, it's readable in two opposite senses. (I first read it as "You've forgotten X - therefore, Y applies, rather than (if you'd remembered X) Z", instead of "You've forgotten to perform action X - X would cause Y to apply, rather than Z") – Damien_The_Unbeliever Jun 18 '12 at 10:56
  • @yasser - Use parameterised queries and make sure the parameters' datatype is `nvarchar` – Martin Smith Jun 18 '12 at 10:56
  • That would be `SqlNVarchar` in c# – Filip De Vos Jun 18 '12 at 10:57
  • Dear @ Martin Smith, what do you mean by parameterised queries ? is there an example? – Yasser Jun 18 '12 at 11:52
  • 1
    @yasser - [There are some examples in the answer to this question](http://stackoverflow.com/q/8747066/73226). Instead of building up the query with the values to be updated injected into the query using string concatenation your query would need to be `UPDATE students SET first_name = @first_name , last_name = @last_name, etc` then you declare the parameters and assign values to them. This is safe against SQL injection, better for re-use of execution plans and avoids datatype issues. – Martin Smith Jun 18 '12 at 12:03
0

Create the database with this collation Arabic_CI_AS, you won't need to put N before the Arabic characters.