9

I have DB which is working with Russian text however when i run queries it shows me this. Database will used by Russians and it has to show Russian text properly!

enter image description here

Any ideas how to fix it? In the future it will located in Russia and work with Russian version SQL Server but right now I am working on English version SQL 2012 Express.

Here is the table and insert statement:

Create table Employee
(
 EmpID int not null IDENTITY (10, 1), 
 StrName nvarchar (25) not null, 
 Phone1 nvarchar (25) not null,
 Phone2 nvarchar (25)
 Primary Key (EmpID),
);
insert into  Employee  (LastName , FirstName,Phone1,Phone2)
  values ('Иванов','111 111 11111','111 111 1111');
Aaron Bertrand
  • 272,866
  • 37
  • 466
  • 490
Andrey
  • 1,629
  • 13
  • 37
  • 65

2 Answers2

21

Are you sure the data has been stored in the database correctly? How do you know?

Make sure that the column has a proper collation, that it is defined as nvarchar and that inserts of string literals are prefixed with N. For example, these are not the same:

INSERT dbo.table(column) SELECT 'foo';
INSERT dbo.table(column) SELECT N'foo';

As an example:

USE tempdb;
GO

CREATE TABLE dbo.foo
(
  ID INT PRIMARY KEY,
  bar NVARCHAR(32) COLLATE SQL_Ukrainian_CP1251_CI_AS
);

INSERT dbo.foo SELECT 1,'АБВГДЕЖЅZЗИІКЛ';
INSERT dbo.foo SELECT 2,N'АБВГДЕЖЅZЗИІКЛ';

SELECT ID, bar FROM dbo.foo;
GO
DROP TABLE dbo.foo;

Results:

ID    bar
----  --------------
1     ????????Z?????
2     АБВГДЕЖЅZЗИІКЛ

And to show how this affects your insert statement, your string is missing the N prefix:

SELECT
  CONVERT(NVARCHAR(32), 'Иванов'),
  CONVERT(NVARCHAR(32), N'Иванов');

Results:

------    ------
??????    Иванов

So, prefix your Unicode strings with N'a prefix' or lose data.

Aaron Bertrand
  • 272,866
  • 37
  • 466
  • 490
  • Thanx! looks like it is working, so do i have to use `N` each time before inserting any text in to every field? – Andrey Aug 20 '13 at 01:52
  • 1
    You need it for any insert into a column that is Unicode (nchar/nvarchar) and when the string literal may contain characters outside the basic ASCII character set. So, in essence: yes. – Aaron Bertrand Aug 20 '13 at 01:53
1

While Aaron Bertrand gave a good explanation why do you getting such a results, I'd say there's a way not to prefix all you strings with russian letters with 'N'.
As far as I know, you have just set your server collation properly. So if you set your collation, for example, like Cyrillic_General_CI_AS, server could treat varchar with russian letters properly:

select
    'español', '平成年月日', 'иван',
    serverproperty('collation')

results:

espanol ?????   иван    Cyrillic_General_CI_AS

As you see, spanish and Chinese strings are not treated properly while russian strings are. So you can insert data into nvarchar columns without prefixing strings with 'N'

That said, I'm using nvarchar data type in our database as default strings, nvarchar parameters in stored procedures. I very rarely use russian strings in code (only when I want to test something), and I've never used N'string' syntax.

While having correct default collation could be handy, there's problem with this solution - it's not easy to change default collation on installed SQL Server, so you have to be careful when installing SQL Server instance and choose collation properly.

袁文涛
  • 735
  • 1
  • 10
  • 23
Roman Pekar
  • 107,110
  • 28
  • 195
  • 197
  • While this can allow you to be lazy in this case, the problem is that [changing the server collation is a pain](http://technet.microsoft.com/en-us/library/ms179254.aspx) (and may cause other problems), and the habit of not prefixing Unicode strings could also lead to problems elsewhere - you're not always going to have the magical ability to change server collation. – Aaron Bertrand Aug 20 '13 at 13:48
  • @AaronBertrand yes, changing server collation is a pain indeed, but this information could be useful to OP – Roman Pekar Aug 20 '13 at 13:51
  • I think your answer should not imply that changing server collation is easier than adding an N prefix to strings that should be prefixed by N regardless of server collation. Or maybe include all of the steps of changing server collation (and the gotchas). Right now it sounds like this is easier, and it's not. – Aaron Bertrand Aug 20 '13 at 13:52