In MySQL, il ASCII() La funzione restituisce il codice numerico ASCII del carattere più a sinistra di una determinata stringa. Fornisci la stringa come argomento.
Questo articolo contiene esempi di utilizzo.
Sintassi
La sintassi è questa:
ASCII(str)
Dove str è la stringa da cui vuoi il codice ASCII del carattere più a sinistra.
Esempio 1 – Utilizzo di base
Ecco un esempio da dimostrare.
SELECT ASCII('MySQL');
Risultato:
+----------------+
| ASCII('MySQL') |
+----------------+
| 77 |
+----------------+
Quindi possiamo vedere che il codice ASCII per la lettera M è 77 .
Per essere assolutamente chiari, prendiamo il codice ASCII per ogni lettera:
SELECT
ASCII('M'),
ASCII('y'),
ASCII('S'),
ASCII('Q'),
ASCII('L');
Risultato:
+------------+------------+------------+------------+------------+
| ASCII('M') | ASCII('y') | ASCII('S') | ASCII('Q') | ASCII('L') |
+------------+------------+------------+------------+------------+
| 77 | 121 | 83 | 81 | 76 |
+------------+------------+------------+------------+------------+
Esempio 2:distinzione tra maiuscole e minuscole
I caratteri maiuscoli hanno un codice ASCII diverso dalle loro controparti minuscole. Esempio:
SELECT
ASCII('m'),
ASCII('M');
Risultato:
+------------+------------+
| ASCII('m') | ASCII('M') |
+------------+------------+
| 109 | 77 |
+------------+------------+
Esempio 3:un esempio di database
Ecco un esempio di utilizzo di ASCII() funzione in una query del database.
USE Music; SELECT AlbumName, ASCII(AlbumName) AS 'ASCII code of leftmost character' FROM Albums LIMIT 10;
Risultato:
+-------------------------+----------------------------------+ | AlbumName | ASCII code of leftmost character | +-------------------------+----------------------------------+ | Powerslave | 80 | | Powerage | 80 | | Singing Down the Lane | 83 | | Ziltoid the Omniscient | 90 | | Casualties of Cool | 67 | | Epicloud | 69 | | Somewhere in Time | 83 | | Piece of Mind | 80 | | Killers | 75 | | No Prayer for the Dying | 78 | +-------------------------+----------------------------------+
Esempio 4 – Personaggio all'estrema destra
In questo esempio restituisco il codice ASCII del più a destra carattere.
USE Music; SELECT AlbumName, RIGHT(AlbumName, 1) 'Rightmost character', ASCII(RIGHT(AlbumName, 1)) 'ASCII code' FROM Albums LIMIT 10;
Risultato:
+-------------------------+---------------------+------------+ | AlbumName | Rightmost character | ASCII code | +-------------------------+---------------------+------------+ | Powerslave | e | 101 | | Powerage | e | 101 | | Singing Down the Lane | e | 101 | | Ziltoid the Omniscient | t | 116 | | Casualties of Cool | l | 108 | | Epicloud | d | 100 | | Somewhere in Time | e | 101 | | Piece of Mind | d | 100 | | Killers | s | 115 | | No Prayer for the Dying | g | 103 | +-------------------------+---------------------+------------+