Is there any work around in sqlite to cover for the lack of "char()" and "ascii()" function ?
For example:
char(97) => 'a'
ascii('a') => 97
Is there any work around in sqlite to cover for the lack of "char()" and "ascii()" function ?
For example:
char(97) => 'a'
ascii('a') => 97
I know this is too late but:
SELECT unicode('a') --ascii('a')
SELECT char(97) --char(97)
I hope this helps :)
Since the time this question was written, SQLite has evidently added a CHAR()
function:
SELECT CHAR(97) -- Result is 'a'
However, the closest I've gotten in the other direction is with the HEX()
function:
SELECT HEX('a') -- Result is 61 (hexadecimal, is equal to 97 decimal)
Obtaining a decimal ASCII
character value seems like it'd require some convoluted work...
I suppose, if you really wanted to, you could create a "ASCII Table" Table of values with a ASCIICHAR
column, and a ASCIICODE
column, and populate it with the ASCII table. Then your lookups could be performed in queries / subqueries:
SELECT ASCIICHAR FROM ASCIITABLE WHERE ASCIICODE = 97;
Really though, Richard J. Ross III's comment is on the money - if you are using SQLite, you are probably accessing it through your calling code, couldn't the computation be done there?
Although conversion is not likely, sqlite does recognize ASCII in this query:
select * from segments where substr(name, 1, 1) < 'A' or substr(name, 1, 1) > 'Z' and substr(name, 1, 1) < 'a';
Possible results: 0-9 and all ASCII < 'a'
It would be trivial to implement sqlite3 user functions ascii() and char() that perform the conversions in C or through another language wrapper that supports sqlite3 user functions (e.g. in python).
http://www.sqlite.org/c3ref/create_function.html
https://docs.python.org/2/library/sqlite3.html
You could implement your own version of ascii based on the sqlite extension library that is on this site: http://sqlite.1065341.n5.nabble.com/Extension-functions-for-SQLite-in-C-for-free-td18942.html
I have modified this code to add functions, not specifically yours though. It should be pretty straightforward.
For older versions use cast(X'61' as text) instead of char(97). 61 is the hexadecimal prepresentation of 97.