开发者

How to SQL compare columns when one has accented chars?

I have two SQLite tables, that 开发者_运维技巧I would love to join them on a name column. This column contains accented characters, so I am wondering how can I compare them for join. I would like the accents dropped for the comparison to work.


You can influence the comparison of characters (such as ignoring case, ignoring accents) by using a Collation. SQLLite has only a few built in collations, although you can add your own.

  • SqlLite, Data types, Collating Sequences
  • SqlLite, Define new Collating Sequence

EDIT: Given that it seems doubtful if Android supports UDFs and computed columns, here's another approach:

  • Add another column to your table, normalizedName
  • When your app writes out rows to your table, it normalizes name itself, removing accents and performing other changes. It saves the result in normalizedName.
  • You use normalizedName in your join.

As the normalization function is now in java, you should have few restrictions in coding it. Several examples for removing accents in java are given here.


There is an easy solution, but not very elegant.

Use the REPLACE function, to remove your accents. Exemple:

SELECT YOUR_COLUMN FROM YOUR_TABLE WHERE replace(replace(replace(replace(replace(replace(replace(replace(
replace(replace(replace( lower(YOUR_COLUMN), 'á','a'), 'ã','a'), 'â','a'), 'é','e'), 'ê','e'), 'í','i'),
'ó','o') ,'õ','o') ,'ô','o'),'ú','u'), 'ç','c') LIKE 'SEARCH_KEY%'

Where SEARCH_KEY is the key word that you wanna find on the column.


As mdma says, a possible solution would be a User-Defined-Function (UDF). There is a document here describing how to create such a function for SQLite in PHP. You could write a function called DROPACCENTS() which drops all the accents in the string. Then, you could join your column with the following code:

SELECT * FROM table1
LEFT JOIN table2
ON DROPACCENTS(table1.column1) = DROPACCENTS(table2.column1)

Much similar to how you would use the UCASE() function to perform a case-insensitive join.

Since you cannot use PHP on Android, you would have to find another way to create the UDF. Although it has been said that creating a UDF is not possible on Android, there is another Stack Overflow article claiming that a content provider could do the trick. The latter sounds slightly complicated, but promising.


Store a special "neutral" column without accented characters and compare / search only this column.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜