简体   繁体   中英

SQLite function that works like the Oracle's “Translate” function?

Oracle has a function called translate that can be used to replace individual characters of the string by others, in the same order that they appear. It is different than the replace function, which replaces the entire second argument occurence by the entire third argument.

translate('1tech23', '123', '456');     --would return '4tech56'
translate('222tech', '2ec', '3it');     --would return '333tith'

I need this to implement a search on a SQLite database ignoring accents (brazilian portuguese language) on my query string. The data in the table that will be queried could be with or without accents, so, depending on how the user type the query string, the results would be different.

Example: Searching for "maçã", the user could type "maca", "maça", "macã" or "maçã", and the data in the table could also be in one of the four possibilities. Using oracle, I would only use this:

Select Name, Id 
  From Fruits 
 Where Translate(Name, 'ãç','ac') = Translate(:QueryString, 'ãç','ac')

... and these other character substitutions:

áéíóúÁÉÍÓÚàèìòùÀÈÌÒÙãõÃÕäëïöüÄËÏÖÜâêîôûÂÊÎÔÛñÑçÇ

by:

aeiouAEIOUaeiouAEIOUaoAOaeiouAEIOUaeiouAEIOUnNcC

Of course I could nest several calls to Replace, but this wouldn't be a good choice.

Thanks in advance by some help.

I don't believe there is anything in sqlite that will translate text in a single pass as you describe.

This wouldn't be difficult to implement as a user defined function however. Here is a decent starting reference .

Open-source Oracle functions for SQLite have been written at Kansas State University. They include translate() (full UTF-8 support, by the way) and can be found here .

I used replace

REPLACE(string,pattern,replacement)

https://www.sqlitetutorial.net/sqlite-replace-function/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM