search

Home  >  Q&A  >  body text

Map sequence of numbers to corresponding letters

<p>I have a <code>int</code> column that contains values ​​no greater than 20. I want to select its corresponding uppercase letter: </p> <pre class="brush:php;toolbar:false;">1 = A 2=B 3=C ...</pre> <p>I don't care what happens after Z because the column does not contain larger values. Is there an easy way to convert this to single byte characters using a SQL query? </p>
P粉304704653P粉304704653453 days ago502

reply all(2)I'll reply

  • P粉269847997

    P粉2698479972023-08-31 11:17:27

    Another MySQL-specific alternative uses elt

    select elt(col,'A','B','C','D','E','F',...);

    Demo

    reply
    0
  • P粉055726146

    P粉0557261462023-08-31 10:12:09

    Add 64 to the integer and you get the ASCII value of the letter you want.

    mysql> select CHAR(1+64);
    +------------+
    | CHAR(1+64) |
    +------------+
    | A          |
    +------------+

    Read https://dev.mysql.com/doc/refman/8.0/en/string-functions.html#function_char

    reply
    0
  • Cancelreply