Inserting arabic characters into sql server database from grails - sql-server

I have a grails application that is intended to insert arabic characters from interface to sql server database. First I did it with field type varchar but it appears ???????. Then I found that the field type should be nvarchar but with, application simply fails to insert any row by giving the following error,
java.sql.SQLException: Cannot insert the value NULL into column 'id', table 'smsCampaigner.dbo.message'; column does not allow nulls. INSERT fails.
I know that this is constraints error that id cannot be null but this was not the case before changing the field type. Now I want to know whether we can specify fro config.groovy which encoding should be used in database ? any other type of help will be appreciated.

here you go..
when you create the domain class use String type for that column and that's it. you could insert any character it will accept.
for example TestDomain.groovy
class TestDomain {
String column1
}

In mysql you can set up the collation of the database/table/column to use utf8_unicode_ci and if I am not wrong that is valid to save arabic chars. Not sure how to do it with SQL Server but it should be something similar.

Change the collation of the database and tables and columns to Arabic_CI_AS
In Sql Server management studio (SSMS), Execute :
ALTER DATABASE DB06 COLLATE Arabic_CI_AS
where DB06 is the name of the database.
right-click on table, then click design, then select the desired column, in its properties, choose the collation to Arabic_CI_AS
by the way, the error :
Cannot insert the value NULL into column
is not related to Arabic, this is because you are trying to insert NULL in the column that does not allow NULL.

Related

DB collation VS Column collation when INSERTing

I've create 2 demo DB's.
Server Collation - Hebrew_CI_AS
DB1 Collation - Hebrew_CI_AS
DB2 Collation - Latin1_General_CS_AS.
In DB2 I have one column with Hebrew_CI_AS Collation. I'm trying to insert Hebrew text into that column. The Datatype is nvarchar(250).
This is the sample script:
INSERT INTO [Table] (HebCol)
VALUES('1בדיקה')
When I run this on DB1, everything works fine.
On DB2, Although the column has Hebrew Collation, I get question marks instead of the Hebrew text.
Why is the result different if the collation is identical?
P.S: I cannot add N before the text. In the real world an app is doing the inserts.
When using literal strings the collation used is that of the database, not the destination column. As the collation of the database you are inserting into is Latin1_General_CS_AS then for the literal string '1בדיקה' most of the characters are outside of the code page of the collation; thus you get ? for those characters as they are unknown.
As such there are only 2 solutions to stop the ? appearing in the column:
Fix your application and define your literal string(s) as an nvarchar not a varchar; you are after all storing an nvarchar so it makes sense to pass a literal nvarchar.
Change the collation of your database to be the same as your other database, Hebrew_CI_AS.
Technically there is a 3rd, which is use a UTF-8 collation if you are on SQL Server 2019, but such collations come with caveats that I don't think are in scope of this question.

Need help joining an Oracle database to SQL Server database when the case sensitivity does not match

I do not have write access to these databases and am not looking to change the entire database's collation. However, I need to join the oracle columns Name and Date to the Sql Server column Name and Date. When I attempt to do them in Tableau, I get a collation error for the Name.
Oracle table contains columns::
'Name' (ANSI/MCBS character string) (sort flag = Case-sensitive)
'Date' (datetime)
'Location' (ANSI/MCBS character string) (sort flag = Case-sensitive)
Sql Server table contains columns:
'Name' (Unicode character string) (sort flag = Case-insensitive)
'Date' (datetime)
'Sale_Type' (Unicode character string) (sort flag = Case-insensitive)
I tried turning the column 'Name' in Oracle to UNISTR(Name) and it did change the character string to Unicode character string. However, the sort flags between the two do not match and it still brings a collation error. SQL Server collation is SQL_Latin1_General_CP1_CI_AS and I tried SQL_Latin1_General_CP1_CS_AS and it did not change the sort to Case Sensitive.
select UNISTR(name) as Name,
Date,
Location
from oracle
select Name,
Date,
Sale_Type
from Sql_Server
I want to be able to join the Name between Oracle and SQL Server without any collation errors.
I can not advise with oracle, but with SQL Server you can change collation during run time. In case if the underlying SQL data source is view, you can change the collation within view definition as follows (use may want to use exact collation that been mentioned in error message to avoid the error):
select Name collate SQL_Latin1_General_CP1_CS_AS as Name_CS,
Date,
Sale_Type
from Sql_Server
When querying across DB link between Oracle and SQL Server you will need to match all upper/lower casing and column names may need to be wrapped in double quotes.

SQL Server query (create rule)

Create a rule called Makedata to allow only the following values to the make of the data: txt, excel, word, rar, and powerpoint.
You must attach the rule to column Make in the datatype table
What does this question mean? I don't want a solution just an explanation.
Thank you
You are asked to filter the values that can be stored in that data column. For this you can use enum data type if were using MySQL. To fit your case take a look at: SQL Server equivalent to MySQL enum data type?
Alter your table with:
column_name VARCHAR(255) NOT NULL CHECK (column_name IN('txt', 'excel', 'word', 'rar', 'powerpoint'))

Arabic Language MS ACCESS

I have an ACCESS database in which characters are Arabic but displayed like this "ÇáÓíÏ ÇáÇãíä ÇáÚÇã ááÍßæãÉ" , is there any solution to this problem please? Thanks
This is a collation problem.
ALTER DATABASE (Database name here) COLLATE Arabic_CI_AS ;
All databases have a default collation (which can be set when creating or altering a database. This collation is used for all metadata in the database, as well as the default for all string columns within the database. Users can choose a different collation for any particular column by using the COLLATE clause.
Go here, for further reading.

2 different collations conflict when merging tables with Sql Server?

I have DB1 which has a Hebrew collation
I also have DB2 which has latin general collation.
I was asked to merge a table (write a query) between DB1.dbo.tbl1 and DB2.dbo.tbl2
I could write in the wuqery
insert into ...SELECT Col1 COLLATE Latin1_General_CI_AS...
But I'm sick of doing it.
I want to make both dbs/tables to the same collation so I don't have to write every time COLLATE...
The question is -
Should I convert latin->hebrew or Hebrew->latin ?
we need to store everything from everything. ( and all our text column are nvarachr(x))
And if so , How do I do it.
If you are using Unicode data types in resulted database - nvarchar(x), then you are to omit COLLATE in INSERT. SQL Server will convert data from your source collation to Unicode automatically. So you should not convert anything if you are inserting to nvarchar column.

Resources