Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert ASCII values to string

Tags:

sql

mysql

I have a table where message is a varchar column type.

mysql> select * from todel;
+---------+
| message |
+---------+
| 73, 116 |
+---------+
1 row in set (0.00 sec)

I am not able to convert the ASCII values to string.

mysql> select char(message) from todel;
+---------------+
| char(message) |
+---------------+
| I             |
+---------------+
1 row in set, 1 warning (0.01 sec)

mysql> show warnings;
+---------+------+----------------------------------------------+
| Level   | Code | Message                                      |
+---------+------+----------------------------------------------+
| Warning | 1292 | Truncated incorrect INTEGER value: '73, 116' |
+---------+------+----------------------------------------------+
1 row in set (0.00 sec)

If I use char function directly, then it works as expected:

mysql> select char(73, 116);
+---------------+
| char(73, 116) |
+---------------+
| It            |
+---------------+
1 row in set (0.00 sec)

How do I use the "char" function while selecting varchar data from a table?

like image 943
shantanuo Avatar asked Apr 06 '26 13:04

shantanuo


1 Answers

You can use SUBSTRING_INDEX() to separate the two strings and then convert them with CHAR()

SELECT CHAR(SUBSTRING_INDEX('73, 116', ',', 1)) --I
SELECT CHAR(SUBSTRING_INDEX('73, 116', ',', -1)) --t
like image 74
Phate01 Avatar answered Apr 09 '26 06:04

Phate01



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!