I tried to store Arabic string in SQL 2008 database but it converted to " question mark " why ? and what should I do ?
You need to choose an Arabic collation for your varchar/char columns or use Unicode (nchar/nvarchar)
CREATE TABLE #test
(
col1 VARCHAR(100) COLLATE Latin1_General_100_CI_AI,
col2 VARCHAR(100) COLLATE Arabic_CI_AI_KS_WS,
col3 NVARCHAR(100)
)
INSERT INTO #test VALUES(N'لا أتكلم العربية',N'لا أتكلم العربية',N'لا أتكلم العربية')
Note the N before values in insert statement above. If you do not mention it, system will treat the values as Varchar, not NVarchar.
SELECT * FROM #test
Returns
col1 col2 col3
------------------------------ ------------------------------ ------------------------------
?? ????? ??????? لا أتكلم العربية لا أتكلم العربية
To see a list of Arabic collations use
SELECT name, description
FROM fn_helpcollations()
WHERE name LIKE 'Arabic%'
All of what you have to do is to make sure that
the column Data type is nvarchar()
after that I inserted Arabic with no problems
You can change the collation on the database level instead of changing for each column in the database:
USE master;
GO
ALTER DATABASE TestDB
COLLATE Arabic_CI_AI;
GO
insert into table (column) values (N'xxx').)
You should put N before string to make it unicode
Add 'N' before every value.
example:
INSERT INTO table1 VALUES(N'aaaaaaaaa',N'ששששששששששששש',N'aaaaaaaaaaa',N'ششششششششششش')
Try using this:
the column Data type is nvarchar()
INSERT INTO CompanyMaster values(N'" + txtCompNameAR.Text + "',N'" + txtCompAddressAR.Text + "','" + txtPh.Text + "')
This is helpful but work here's what works for me in all cases
ALTER DATABASE [database] SET SINGLE_USER WITH ROLLBACK IMMEDIATE;
GO
ALTER DATABASE [database] COLLATE ARABIC_CI_AS;
GO
ALTER DATABASE [database] SET MULTI_USER;
GO
update: eventually I have to change datatype varchar to nvarchar in my project
make sure all your tables and varchar columns have the collation of utf8_general_ci
Iti is easy to store Arabic string in Oracle. Use this code:
declare #P_CUSTOMER_NAME nchar(50)
set #P_CUSTOMER_NAME2=N'أختبار'
The above will save in Oracle just fine.
Related
So trying to remove all the extended ascii characters and used collation SQL_Latin1_General_CP1253_CI_AI in the ddl but still getting ascii characters. Is there any suggestion ?
I have a value stored in the sql as 'àccõrd' and i want it to be stored as accord. When i try select àccõrd collate SQL_Latin1_General_CP1253_CI_AI it works but when i load it still gets loaded as àccõrd
Here are two ways to achieve what you're trying to do...
First method: define the column with a specific collation, e.g.:
create table dbo.Foo (
FooName varchar(50) collate SQL_Latin1_General_CP1253_CI_AI
);
insert dbo.Foo (FooName) values ('àccõrd');
select FooName from dbo.Foo;
Which yields:
FooName
-------
accord
Second method: collate the text when inserting it into your table:
-- Display the SQL Server instance's "default collation," configured during setup.
-- e.g.: SQL_Latin1_General_CP1_CI_AS
select serverproperty('collation') as ServerCollation;
-- Display the current database's "default collation," configured in CREATE DATABASE.
-- e.g.: SQL_Latin1_General_CP1_CI_AS
select collation_name
from sys.databases
where [name]=db_name();
create table dbo.Bar (
BarName varchar(50) --database default: collate SQL_Latin1_General_CP1_CI_AS
);
insert dbo.Bar (BarName) values ('àccõrd' collate SQL_Latin1_General_CP1253_CI_AI);
select BarName from dbo.Bar;
Which yields:
BarName
-------
accord
NOTE: These collation tricks only work with char and varchar data types.
I facing a weird issue in SQL. Im trying to save these characters ❷❶❸ into SQL. But its storing as Question Marks (?). The field is nVarchar.
This is my update query
update mytable set keywords='key1❶,key2❶,key3❶,key4❶' where id=50543
The column should be created as
CREATE TABLE mytable (columnname NVARCHAR(40) COLLATE SQL_Latin1_General_CP1253_CI_AI)
Then when insert use prefix Unicode character string
INSERT INTO mytable (columnname) VALUES (N'❷❶❸')
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.
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.
In Object Explorer, connect to an instance of the SQL Server Database Engine, expand that instance, and then expand Databases.
Right-click the database that you want and click Properties.
Click the Options page, and select a collation from the Collation
drop-down list.
After you are finished, click OK.
MORE INFO
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
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.
Change type of your columns (containing Russian) from varchar to nvarchar.
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!
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');
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.
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.
I'm having a problem in a VB.NET application, i have a form that the user fills to store data in the database, the problem is that when i enter Arabic string, it gets stored in the database as question marks: ??????
The database field type is nvarchar, has anybody else come across this problem?
take a look here seach for Database-level collations.
You need to choose an Arabic collation for your varchar/char columns or use Unicode (nchar/nvarchar)
CREATE TABLE #test
(
col1 VARCHAR(100) COLLATE Latin1_General_100_CI_AI,
col2 VARCHAR(100) COLLATE Arabic_CI_AI_KS_WS,
col3 NVARCHAR(100)
)
INSERT INTO #test VALUES(N'لا أتكلم العربية',N'لا أتكلم العربية',N'لا أتكلم العربية')