I am storing arabic text in sqlite database in my wp8 app. Below is an example of the arabic text stored in the sqlite file.
ٱلْحَمْدُ لِلَّهِ رَبِّ ٱلْعَٰلَمِينَ
The user would be able to search like below:
الحمد
this should return the above text, but I know in normal it doesn't.
I am using very simple query in sqlite
select * from tbl where ArabicText like '%الحمد%'
and in c#
query = "select * from tbl where ArabicText like '%"+textToSearch+"%'"
The result is zero records, How should I search to retrieve the above single record?
Thanks!
The easy answer, is to just use base64_encode on queries going into the database, and decode on information coming out (both while saving and while searching). It's not elegant, but it allows UTF characters into a non-UTF database.
Related
I'm trying to create a script that insert some rows in a table that has an xml saved as IMAGE type. The xml is using utf-8 format.
When I test with some text with accents, I just got the "illegal character" error on XML reading (via XmlDocument in c#).
So I want some way to convert an string with accent to a utf-8 format.
Example: when I save via XmlWriter (in c#) a string that contains "ã", it auto-converts it to "ã" to represent that character when saving on the database. And it works perfectly!
But I want to do it in a SQL Script.
So let's say the word "São Paulo", it saves "São Paulo". But using only SQL Server (script), I just can't make the xml valid with the word "São Paulo".
Only for more tech details, I'm working with a specific varchar/nvarchar that has this problem, so I just need to convert an text to a "unicode" or "utf-8" text that is valid on xml reading. And using Oracle worked fine, as it saves with accent and reads normally (using BLOB type), but I need the same script on SQL Server too.
I already tried some collates, converts and casts that I've found on the web but nothing worked for me.
Anyone know anything I can do?
Note: a thousand of replaces is not a solution.
Please try this:
CREATE TABLE testXML(test XML);
GO
INSERT INTO testXML(test) VALUES ('<root><r>plain text</r></root>')
,('<root><r>São Paulo</r></root>');
GO
SELECT x.y.query('.'), x.y.value('.','varchar(max)')
FROM testXML
CROSS APPLY test.nodes('/root/r') AS x(y);
GO
DROP TABLE testXML;
/*
Result
<r>plain text</r> plain text
<r>São Paulo</r> São Paulo
*/
Here the same with IMAGE and a lot of casting around. delivers the same result:
CREATE TABLE testXML(test IMAGE);
GO
INSERT INTO testXML(test) VALUES (CAST(CAST('<root><r>plain text</r></root>' AS XML) AS VARBINARY(MAX)))
,(CAST(CAST('<root><r>São Paulo</r></root>' AS XML) AS VARBINARY(MAX)));
GO
SELECT x.y.query('.'), x.y.value('.','varchar(max)')
FROM testXML
CROSS APPLY (SELECT CAST(CAST(test AS VARBINARY(MAX)) AS XML)) AS tbl(x)
CROSS APPLY tbl.x.nodes('/root/r') AS x(y);
GO
DROP TABLE testXML;
I have an existing SQL Server database where text is stored in Arabic. The default database collation is FRENCH_CI_AS, but the application uses Arabic. Displaying the data with ASP is not a problem, but I want to create a new database using UTF-8!
Text sample as it's stored in database :
ترأس وزير السكن والعمران ووزير الأشغال العمومية للجنة التقنية لمراقبة البناء
How I can transform text to get clear Arabic text in the database ?
Is there a solution using Excel? http://en.file-upload.net/download-10245297/test.xls.html
first of all use nvarchar() for type of Data in your Tables then when inserting data into your tabel insert like this
string Query="insert into tablename(columnName) values(N'value')...";
The strings need to be stored in the database as NVARCHAR instead of VARCHAR. This allows the database to store UTF16 encoded strings instead of ASCII with a CodePage. Of course this will double the amount of storage needed for the database.
From the screen shot it looks like the string is UTF8 being displayed as if it was ASCII and there does not appear to be a way to tell SQL this detail.
I share a little java project (with dependencies).
This project loads table data first and formats strings. The generated EXCEL worksheet can now imported using SSMS.
Java solution :
String charabia = "ترأس وزير السكن والعمر" ;
try {
String utf8String = new String(charabia.getBytes(), "UTF-8");
} catch (UnsupportedEncodingException e) {
}
My project download link : here
I have an existing database with existing data that I can't change it's structure or values.
In that database there is a nvarchar column that contains values in the twilight unicode zone starting with F800, upward.
When I select those values in SQL or use SQL function, unicode - I get the proper values.
When I select the same values in .Net - I get an error value - all the values in that twilight zone become 65533.
I need those values - how can I presuade .Net to give me those values - something like chaninging the connection encoding to a custom one - or ucs-2 etc...
Here is a sample code that demonstraits the problem:
c.CommandText = "select NCHAR(55297)";
using (var r = c.ExecuteReader())
{
r.Read();
var result = r[0]; //expected 55297 but got 65533
}
55297 is D801 which isn't defined? you probably want f801 which is 63489? But it appears as if that one isn't defined either. Which characters do you want?
If I try doing a "select NCHAR(55297)" in SQL Server Management studio, I get back the diamond question mark, but if I do "select NCHAR(63489)" I get back a dot of some sort:
If what you want is the character values, you can ask for them directly:
select Unicode(NCHAR(63489))
This returns 63489 (as an integer)
If you want them as a byte array, you can ask for that:
select CONVERT(varbinary(MAX), FieldThatIsAnNvarchar) from ThatTable
After much investigations I failed to find any way around this. I couldn't find any two way conversion that would work here.
It seems that some unicode values are intended for some strange unicode scenario that isn't supported by .Net, but is partially supported in a way that breaks what we need here.
I'm using SQLite to compare some ids (varchar 50) so I get a numeric database id.
I'm using C# and visual studio 2003. Pretty new in C# so maybe I'm doing a newbie mistake.
When doing some comparison like the following
SELECT * FROM tblUSer WHERE Use_id like '%Ñ120%'
I don't get anything even if this exist... I suppose is an encoding problem, but I don't know how to solve it.
I can't change the database schema since is not my database (need to modify select some data and update a field).
I can't change the data per se since it is a data that already exist and the user codes are the same as some reference like a club id or something...
Changing the data to UTF-8 worked to make it do the query (without it it would give an error) but still I don't get the desired data. Maybe is also how the data is saved by the original program.
What do I do so my query works with the Ñ?
What if your remove the N from your query and just check
Use_id LIKE '%120%'
and then select the appropriate ones in C#?
try this:
SELECT * FROM tblUSer WHERE Use_id like N'%Ñ120%'
Notice the N letter.
You should write in this way:
SELECT * FROM tblUSer WHERE Use_id like N'%Ñ120%'
In the application I am working on, I have a TableAdapter, and a RadGridView (Telerik control - their version of the DataGridView). The TA is pulling different elements from our database, including PhoneNumber, which is stored as varchar.
In my GridView, I would like to display this format as (123) 456-7890. Unfortunately, I have been unable to accomplish this.
I have tried the following (which is Telerik's recommended method):
radGvUsers.Columns["PhoneNumber"].FormatString = "{0: (###) ###-####}";
This results in the value displaying as it does in the db: 1234567890
I know (ok, I think) this is because it is stored as varchar and needs to be converted to a number, but I don't know how or where (i.e. at the TA or GridView level) best to do this.
Any suggestions? Feel free to ask more questions if I did not supply enough info!
You can convert the string to a numeric in the SQL Statement
Select Convert(bigint, PhoneNumber) AS PhoneNumber FROM sometable
You can easily do like below. No need to get data as string from database.
radGvUsers.Columns["PhoneNumber"].DefaultCellStyle.Format = "(###) ###-####";