In MariaDB, MID() è sinonimo di SUBSTRING() . Restituisce una sottostringa da una determinata stringa.
Secondo la documentazione di MariaDB è sinonimo di SUBSTRING(str,pos,len) sintassi di SUBSTRING() , tuttavia, i miei test dimostrano che funziona con tutti i vari moduli.
Sintassi
Secondo la documentazione di MariaDB, la sintassi è questa:
MID(str,pos,len)
Tuttavia, ho scoperto che funziona con i seguenti moduli (che sono gli stessi che SUBSTRING() supporta):
MID(str,pos),
MID(str FROM pos),
MID(str,pos,len),
MID(str FROM pos FOR len)
Dove str è la stringa, pos è la posizione iniziale della sottostringa e len è il numero di caratteri da estrarre.
Esempio
Ecco un esempio di base:
SELECT MID('Drink your beer', 7); Risultato:
+---------------------------+
| MID('Drink your beer', 7) |
+---------------------------+
| your beer |
+---------------------------+
Ecco lo stesso esempio, ma usando il FROM sintassi:
SELECT MID('Drink your beer' FROM 7); Risultato:
+-------------------------------+
| MID('Drink your beer' FROM 7) |
+-------------------------------+
| your beer |
+-------------------------------+ Lunghezza sottostringa
Ecco un esempio che specifica la lunghezza della sottostringa da estrarre. Questa è l'unica sintassi che la documentazione di MariaDB cita per MID() funzione.
SELECT MID('Drink your beer', 7, 4); Risultato:
+------------------------------+
| MID('Drink your beer', 7, 4) |
+------------------------------+
| your |
+------------------------------+
E qui sta usando FROM...FOR sintassi:
SELECT MID('Drink your beer' FROM 7 FOR 4); Risultato:
+-------------------------------------+
| MID('Drink your beer' FROM 7 FOR 4) |
+-------------------------------------+
| your |
+-------------------------------------+ Posizione negativa
Specificando un valore negativo per la posizione, la posizione iniziale viene contata all'indietro dalla fine della stringa:
SELECT MID('Drink your beer', -9); Risultato:
+----------------------------+
| MID('Drink your beer', -9) |
+----------------------------+
| your beer |
+----------------------------+
Una posizione negativa può essere utilizzata anche quando si utilizza il FROM sintassi:
SELECT MID('Drink your beer' FROM -9 FOR 4); Risultato:
+--------------------------------------+
| MID('Drink your beer' FROM -9 FOR 4) |
+--------------------------------------+
| your |
+--------------------------------------+ In questo caso ho anche impostato una lunghezza per la sottostringa.
Modalità Oracle
In modalità Oracle, una posizione iniziale di 0 (zero) viene trattato come 1 . Tuttavia, una posizione iniziale di 1 viene anche trattato come 1 .
Questo è in contrasto con altre modalità, dove 0 restituirà una stringa vuota.
Esempio:
SET SQL_MODE=ORACLE;
SELECT
MID('Drink your beer', 0) AS "0",
MID('Drink your beer', 1) AS "1"; Risultato:
+-----------------+-----------------+ | 0 | 1 | +-----------------+-----------------+ | Drink your beer | Drink your beer | +-----------------+-----------------+
Eccolo in modalità predefinita:
SET SQL_MODE=DEFAULT;
SELECT
MID('Drink your beer', 0) AS "0",
MID('Drink your beer', 1) AS "1"; Risultato:
+------+-----------------+ | 0 | 1 | +------+-----------------+ | | Drink your beer | +------+-----------------+
Argomenti nulli
Se uno (o tutti) gli argomenti sono null , il MID() la funzione restituisce null :
SELECT
MID(null, 3, 3),
MID('Beer', null, 3),
MID('Beer', 3, null),
MID(null, null, null); Risultato:
+-----------------+----------------------+----------------------+-----------------------+
| MID(null, 3, 3) | MID('Beer', null, 3) | MID('Beer', 3, null) | MID(null, null, null) |
+-----------------+----------------------+----------------------+-----------------------+
| NULL | NULL | NULL | NULL |
+-----------------+----------------------+----------------------+-----------------------+ Argomenti mancanti
Chiamando MID() senza passare alcun argomento genera un errore:
SELECT MID(); Risultato:
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near ')' at line 1