Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Select cyrillic character in SQL

When user insert Russian word like 'пример' to database,database saves it like '??????'. If they insert with 'N' letter or I select it with 'N' letter, ie; exec Table_Name N'иытание' there is no problem. But I don't want to use 'N' in every query, so is there any solution for this? I will use stored procedure by the way.

UPDATE:

Now I can use Russian letters with alter collation. But I can't alter collation for every language and I just want to learn is there any trigger or function for automatic add N in front of the text after text add. IE; when I insert 'пример', SQL should take it like N'пример' autamaticly.

like image 573
Kerem Celebi Avatar asked Sep 02 '15 10:09

Kerem Celebi


3 Answers

You have to use column's datatype NVARCHAR to insert unicode letters, also you have to use N'value' when inserting.

You can test it in following:

CREATE TABLE #test
(
    varcharCol varchar(40),
    nvarcharCol nvarchar(40)
)
INSERT INTO #test VALUES (N'иытание', N'иытание')

SELECT * FROM #test

OUTPUT

varcharCol  nvarcharCol
???????     иытание

As you see column of datatype varchar returning questionmarks ?????? and column of datatype nvarchar returning russian characters иытание.


UPDATE

Problem is that your database collation does not support russian letters.

  1. In Object Explorer, connect to an instance of the SQL Server Database Engine, expand that instance, and then expand Databases.
  2. Right-click the database that you want and click Properties.
  3. Click the Options page, and select a collation from the Collation drop-down list.
  4. After you are finished, click OK.

MORE INFO

like image 172
Stanislovas Kalašnikovas Avatar answered Nov 15 '22 00:11

Stanislovas Kalašnikovas


it would very difficult to put in comment i would recommend this link Info

declare @test TABLE 
(
    Col1 varchar(40),
    Col2 varchar(40),
    Col3 nvarchar(40),
    Col4 nvarchar(40)
)
INSERT INTO @test VALUES
('иытание',N'иытание','иытание',N'иытание')

SELECT * FROM @test

RESULT

enter image description here

like image 25
wiretext Avatar answered Nov 14 '22 22:11

wiretext


To store and select Unicode character in database you have to use NVARCHAR instead of VARCHAR. To insert Unicode data you have to use N

See this link https://technet.microsoft.com/en-us/library/ms191200%28v=sql.105%29.aspx

The n prefix for these data types comes from the ISO standard for National (Unicode) data types.

like image 26
Ajay Avatar answered Nov 15 '22 00:11

Ajay