SQL - Funzioni stringa

Le funzioni stringa SQL vengono utilizzate principalmente per la manipolazione delle stringhe. La tabella seguente descrive in dettaglio le importanti funzioni di stringa:

Sr.No. Descrizione della funzione
1 ASCII ()

Restituisce il valore numerico del carattere più a sinistra

2 BIDONE()

Restituisce una rappresentazione di stringa dell'argomento

3 BIT_LENGTH ()

Restituisce la lunghezza dell'argomento in bit

4 CHAR_LENGTH ()

Restituisce il numero di caratteri nell'argomento

5 CHAR ()

Restituisce il carattere per ogni numero intero passato

6 CHARACTER_LENGTH ()

Un sinonimo di CHAR_LENGTH ()

7 CONCAT_WS ()

Restituisce concatenato con separatore

8 CONCAT ()

Restituisce una stringa concatenata

9 CONV ()

Converte i numeri tra diverse basi numeriche

10 ELT ()

Restituisce una stringa al numero di indice

11 EXPORT_SET ()

Restituisce una stringa tale che per ogni bit impostato nel valore bits, ottieni una stringa on e per ogni bit non impostato, ottieni una stringa off

12 CAMPO()

Restituisce l'indice (posizione) del primo argomento negli argomenti successivi

13 FIND_IN_SET ()

Restituisce la posizione di indice del primo argomento all'interno del secondo argomento

14 FORMATO()

Restituisce un numero formattato con il numero di posizioni decimali specificato

15 ESADECIMALE()

Restituisce una rappresentazione di stringa di un valore esadecimale

16 INSERIRE()

Inserisce una sottostringa nella posizione specificata fino al numero di caratteri specificato

17 INSTR ()

Restituisce l'indice della prima occorrenza della sottostringa

18 LCASE ()

Sinonimo di INFERIORE ()

19 SINISTRA()

Restituisce il numero di caratteri più a sinistra come specificato

20 LUNGHEZZA()

Restituisce la lunghezza di una stringa in byte

21 LOAD_FILE ()

Carica il file denominato

22 INDIVIDUARE()

Restituisce la posizione della prima occorrenza della sottostringa

23 INFERIORE()

Restituisce l'argomento in minuscolo

24 LPAD ()

Restituisce l'argomento della stringa, riempito a sinistra con la stringa specificata

25 LTRIM ()

Rimuove gli spazi iniziali

26 MAKE_SET ()

Restituisce un insieme di stringhe separate da virgole con il bit corrispondente in bit impostato

27 MID ()

Restituisce una sottostringa a partire dalla posizione specificata

28 OTTOBRE ()

Restituisce una rappresentazione di stringa dell'argomento ottale

29 OCTET_LENGTH ()

Un sinonimo di LENGTH ()

30 ORD ()

Se il carattere più a sinistra dell'argomento è un carattere multibyte, restituisce il codice per quel carattere

31 POSIZIONE()

Un sinonimo di LOCATE ()

32 CITAZIONE()

Evita l'argomento per l'uso in un'istruzione SQL

33 REGEXP

Pattern matching utilizzando espressioni regolari

34 RIPETERE()

Ripete una stringa il numero di volte specificato

35 SOSTITUIRE()

Sostituisce le occorrenze di una stringa specificata

36 INVERSIONE()

Inverte i caratteri in una stringa

37 DESTRA()

Restituisce il numero di caratteri più a destra specificato

38 RPAD ()

Aggiunge la stringa il numero di volte specificato

39 RTRIM ()

Rimuove gli spazi finali

40 SOUNDEX ()

Restituisce una stringa soundex

41 MI PIACE

Confronta i suoni

42 SPAZIO()

Restituisce una stringa del numero di spazi specificato

43 STRCMP ()

Confronta due stringhe

44 SUBSTRING_INDEX ()

Restituisce una sottostringa da una stringa prima del numero specificato di occorrenze del delimitatore

45 SUBSTRING (), SUBSTR ()

Restituisce la sottostringa come specificato

46 TRIM ()

Rimuove gli spazi iniziali e finali

47 UCASE ()

Sinonimo di UPPER ()

48 UNHEX ()

Converte ogni coppia di cifre esadecimali in un carattere

49 SUPERIORE()

Converte in maiuscolo

ASCII (str)

Restituisce il valore numerico del carattere più a sinistra della stringa str. Restituisce 0 se str è la stringa vuota. Restituisce NULL se str è NULL. ASCII () funziona per i caratteri con valori numerici compresi tra 0 e 255.

SQL> SELECT ASCII('2');
+---------------------------------------------------------+
| ASCII('2')                                              |
+---------------------------------------------------------+
| 50                                                      |
+---------------------------------------------------------+
1 row in set (0.00 sec)

SQL> SELECT ASCII('dx');
+---------------------------------------------------------+
| ASCII('dx')                                             |
+---------------------------------------------------------+
| 100                                                     |
+---------------------------------------------------------+
1 row in set (0.00 sec)

BIN (N)

Restituisce una rappresentazione in formato stringa del valore binario di N, dove N è un numero lungo (BIGINT). Ciò è equivalente a CONV (N, 10,2). Restituisce NULL se N è NULL.

SQL> SELECT BIN(12);
+---------------------------------------------------------+
| BIN(12)                                                 |
+---------------------------------------------------------+
| 1100                                                    |
+---------------------------------------------------------+
1 row in set (0.00 sec)

BIT_LENGTH (str)

Restituisce la lunghezza della stringa str in bit.

SQL> SELECT BIT_LENGTH('text');
+---------------------------------------------------------+
| BIT_LENGTH('text')                                      |
+---------------------------------------------------------+
| 32                                                      |
+---------------------------------------------------------+
1 row in set (0.00 sec)

CHAR (N, ... [USING charset_name])

CHAR () interpreta ogni argomento N come un numero intero e restituisce una stringa composta dai caratteri dati dai valori del codice di tali numeri interi. I valori NULL vengono ignorati.

SQL> SELECT CHAR(77,121,83,81,'76');
+---------------------------------------------------------+
| CHAR(77,121,83,81,'76')                                 |
+---------------------------------------------------------+
| MySQL                                                   |
+---------------------------------------------------------+
1 row in set (0.00 sec)

CHAR_LENGTH (str)

Restituisce la lunghezza della stringa str misurata in caratteri. Un carattere multibyte conta come un singolo carattere. Ciò significa che per una stringa contenente cinque caratteri a due byte, LENGTH () restituisce 10, mentre CHAR_LENGTH () restituisce 5.

SQL> SELECT CHAR_LENGTH("text");
+---------------------------------------------------------+
| CHAR_LENGTH("text")                                     |
+---------------------------------------------------------+
| 4                                                       |
+---------------------------------------------------------+
1 row in set (0.00 sec)

CHARACTER_LENGTH (str)

CHARACTER_LENGTH () è un sinonimo di CHAR_LENGTH ().

CONCAT (str1, str2, ...)

Restituisce la stringa che risulta dalla concatenazione degli argomenti. Può contenere uno o più argomenti. Se tutti gli argomenti sono stringhe non binarie, il risultato è una stringa non binaria. Se gli argomenti includono stringhe binarie, il risultato è una stringa binaria. Un argomento numerico viene convertito nella sua forma di stringa binaria equivalente; se vuoi evitarlo, puoi usare un cast di tipo esplicito, come in questo esempio -

SQL> SELECT CONCAT('My', 'S', 'QL');
+---------------------------------------------------------+
| CONCAT('My', 'S', 'QL')                                 |
+---------------------------------------------------------+
| MySQL                                                   |
+---------------------------------------------------------+
1 row in set (0.00 sec)

CONCAT_WS (separatore, str1, str2, ...)

CONCAT_WS () sta per Concatenate With Separator ed è una forma speciale di CONCAT (). Il primo argomento è il separatore per il resto degli argomenti. Il separatore viene aggiunto tra le stringhe da concatenare. Il separatore può essere una stringa, così come il resto degli argomenti. Se il separatore è NULL, il risultato è NULL.

SQL> SELECT CONCAT_WS(',','First name','Last Name' );
+---------------------------------------------------------+
| CONCAT_WS(',','First name','Last Name' )                |
+---------------------------------------------------------+
| First name,Last Name                                   |
+---------------------------------------------------------+
1 row in set (0.00 sec)

CONV (N, from_base, to_base)

Converte i numeri tra diverse basi numeriche. Restituisce una rappresentazione di stringa del numero N, convertito da base from_base a to_base. Restituisce NULL se un argomento è NULL. L'argomento N viene interpretato come un numero intero, ma può essere specificato come un numero intero o una stringa. La base minima è 2 e la base massima è 36. Se to_base è un numero negativo, N è considerato un numero con segno. In caso contrario, N viene considerato non firmato. CONV () funziona con una precisione a 64 bit.

SQL> SELECT CONV('a',16,2);
+---------------------------------------------------------+
| CONV('a',16,2)                                          |
+---------------------------------------------------------+
| 1010                                                    |
+---------------------------------------------------------+
1 row in set (0.00 sec)

ELT (N, str1, str2, str3, ...)

Restituisce str1 se N = 1, str2 se N = 2 e così via. Restituisce NULL se N è minore di 1 o maggiore del numero di argomenti. ELT () è il complemento di FIELD ().

SQL> SELECT ELT(1, 'ej', 'Heja', 'hej', 'foo');
+---------------------------------------------------------+
| ELT(1, 'ej', 'Heja', 'hej', 'foo')                      |
+---------------------------------------------------------+
| ej                                                      |
+---------------------------------------------------------+
1 row in set (0.00 sec)

EXPORT_SET (bit, on, off [, separator [, number_of_bits]])

Restituisce una stringa tale che per ogni bit impostato nel valore bits, si ottiene una stringa on e per ogni bit non impostato nel valore, si ottiene una stringa off. I bit in bit vengono esaminati da destra a sinistra (da bit di ordine basso a bit di ordine elevato). Le stringhe vengono aggiunte al risultato da sinistra a destra, separate dalla stringa di separazione (l'impostazione predefinita è il carattere virgola.,.). Il numero di bit esaminati è dato da number_of_bits (il valore predefinito è 64).

SQL> SELECT EXPORT_SET(5,'Y','N',',',4);
+---------------------------------------------------------+
| EXPORT_SET(5,'Y','N',',',4)                             |
+---------------------------------------------------------+
| Y,N,Y,N                                                 |
+---------------------------------------------------------+
1 row in set (0.00 sec)

CAMPO (str, str1, str2, str3, ...)

Restituisce l'indice (posizione che inizia con 1) di str nell'elenco str1, str2, str3, ... Restituisce 0 se str non viene trovato.

SQL> SELECT FIELD('ej', 'Hej', 'ej', 'Heja', 'hej', 'foo');
+---------------------------------------------------------+
| FIELD('ej', 'Hej', 'ej', 'Heja', 'hej', 'foo')          |
+---------------------------------------------------------+
| 2                                                       |
+---------------------------------------------------------+
1 row in set (0.00 sec)

FIND_IN_SET (str, strlist)

Restituisce un valore compreso tra 1 e N se la stringa str si trova nell'elenco di stringhe strlist costituito da N sottostringhe.

SQL> SELECT FIND_IN_SET('b','a,b,c,d');
+---------------------------------------------------------+
| SELECT FIND_IN_SET('b','a,b,c,d')                       |
+---------------------------------------------------------+
| 2                                                       |
+---------------------------------------------------------+
1 row in set (0.00 sec)

FORMATO (X, D)

Formatta il numero X in un formato come "#, ###, ###. ##", arrotondato alle cifre decimali D e restituisce il risultato come stringa. Se D è 0, il risultato non ha punto decimale o parte frazionaria.

SQL> SELECT FORMAT(12332.123456, 4);
+---------------------------------------------------------+
| FORMAT(12332.123456, 4)                                 |
+---------------------------------------------------------+
| 12,332.1235                                             |
+---------------------------------------------------------+
1 row in set (0.00 sec)

HEX (N_o_S)

Se N_or_S è un numero, restituisce una rappresentazione di stringa del valore esadecimale di N, dove N è un numero lungo (BIGINT). Ciò è equivalente a CONV (N, 10,16).

Se N_or_S è una stringa, restituisce una rappresentazione di stringa esadecimale di N_or_S dove ogni carattere in N_or_S viene convertito in due cifre esadecimali.

SQL> SELECT HEX(255);
+---------------------------------------------------------+
| HEX(255)                                                |
+---------------------------------------------------------+
| FF                                                      |
+---------------------------------------------------------+
1 row in set (0.00 sec)

SQL> SELECT 0x616263;
+---------------------------------------------------------+
| 0x616263                                                |
+---------------------------------------------------------+
| abc                                                     |
+---------------------------------------------------------+
1 row in set (0.00 sec)

INSERT (str, pos, len, newstr)

Restituisce la stringa str, con la sottostringa che inizia alla posizione pos e i caratteri len long sostituiti dalla stringa newstr. Restituisce la stringa originale se pos non rientra nella lunghezza della stringa. Sostituisce il resto della stringa dalla posizione pos se len non rientra nella lunghezza del resto della stringa. Restituisce NULL se un argomento è NULL.

SQL> SELECT INSERT('Quadratic', 3, 4, 'What');
+---------------------------------------------------------+
| INSERT('Quadratic', 3, 4, 'What')                       |
+---------------------------------------------------------+
| QuWhattic                                               |
+---------------------------------------------------------+
1 row in set (0.00 sec)

INSTR (str, substr)

Restituisce la posizione della prima occorrenza della sottostringa substr nella stringa str. È uguale alla forma a due argomenti di LOCATE (), tranne per il fatto che l'ordine degli argomenti è invertito.

SQL> SELECT INSTR('foobarbar', 'bar');
+---------------------------------------------------------+
| INSTR('foobarbar', 'bar')                               |
+---------------------------------------------------------+
| 4                                                       |
+---------------------------------------------------------+
1 row in set (0.00 sec)

LCASE (str)

LCASE () è un sinonimo di LOWER ().

SINISTRA (str, len)

Restituisce i caratteri len più a sinistra dalla stringa str o NULL se un argomento è NULL.

SQL> SELECT LEFT('foobarbar', 5);
+---------------------------------------------------------+
| LEFT('foobarbar', 5)                                    |
+---------------------------------------------------------+
| fooba                                                   |
+---------------------------------------------------------+
1 row in set (0.00 sec)

LUNGHEZZA (str)

Restituisce la lunghezza della stringa str, misurata in byte. Un carattere multibyte conta come più byte. Ciò significa che per una stringa contenente cinque caratteri a due byte, LENGTH () restituisce 10, mentre CHAR_LENGTH () restituisce 5.

SQL> SELECT LENGTH('text');
+---------------------------------------------------------+
| LENGTH('text')                                          |
+---------------------------------------------------------+
| 4                                                       |
+---------------------------------------------------------+
1 row in set (0.00 sec)

LOAD_FILE (nome_file)

Legge il file e restituisce il contenuto del file come una stringa. Per utilizzare questa funzione, il file deve trovarsi sull'host del server, è necessario specificare il percorso completo del file ed è necessario disporre del privilegio FILE. Il file deve essere leggibile da tutti e le sue dimensioni sono inferiori a max_allowed_packet byte.

Se il file non esiste o non può essere letto perché una delle condizioni precedenti non è soddisfatta, la funzione restituisce NULL.

A partire da SQL 5.0.19, la variabile di sistema character_set_filesystem controlla l'interpretazione dei nomi di file forniti come stringhe letterali.

SQL> UPDATE table_test
   -> SET blob_col=LOAD_FILE('/tmp/picture')
	-> WHERE id=1;
...........................................................

LOCATE (substr, str), LOCATE (substr, str, pos)

La prima sintassi restituisce la posizione della prima occorrenza della sottostringa substr nella stringa str. La seconda sintassi restituisce la posizione della prima occorrenza della sottostringa substr nella stringa str, a partire dalla posizione pos. Restituisce 0 se substr non è in str.

SQL> SELECT LOCATE('bar', 'foobarbar');
+---------------------------------------------------------+
| LOCATE('bar', 'foobarbar')                              |
+---------------------------------------------------------+
| 4                                                       |
+---------------------------------------------------------+
1 row in set (0.00 sec)

INFERIORE (str)

Restituisce la stringa str con tutti i caratteri modificati in minuscolo in base alla mappatura del set di caratteri corrente.

SQL> SELECT LOWER('QUADRATICALLY');
+---------------------------------------------------------+
| LOWER('QUADRATICALLY')                                  |
+---------------------------------------------------------+
| quadratically                                           |
+---------------------------------------------------------+
1 row in set (0.00 sec)

LPAD (str, len, padstr)

Restituisce la stringa str, riempita a sinistra con la stringa padstr a una lunghezza di caratteri len. Se str è più lungo di len, il valore restituito viene abbreviato in caratteri len.

SQL> SELECT LPAD('hi',4,'??');
+---------------------------------------------------------+
| LPAD('hi',4,'??')                                       |
+---------------------------------------------------------+
| ??hi                                                    |
+---------------------------------------------------------+
1 row in set (0.00 sec)

LTRIM (str)

Restituisce la stringa str con i caratteri di spazio iniziali rimossi.

SQL> SELECT LTRIM('  barbar');
+---------------------------------------------------------+
| LTRIM('  barbar')                                       |
+---------------------------------------------------------+
| barbar                                                  |
+---------------------------------------------------------+
1 row in set (0.00 sec)

MAKE_SET (bit, str1, str2, ...)

Restituisce un valore impostato (una stringa contenente sottostringhe separate da.,. Caratteri) costituito dalle stringhe che hanno il bit corrispondente in bit impostato. str1 corrisponde al bit 0, str2 al bit 1 e così via. I valori NULL in str1, str2, ... non vengono aggiunti al risultato.

SQL> SELECT MAKE_SET(1,'a','b','c');
+---------------------------------------------------------+
| MAKE_SET(1,'a','b','c')                                 |
+---------------------------------------------------------+
| a                                                       |
+---------------------------------------------------------+
1 row in set (0.00 sec)

MID (str, pos, len)

MID (str, pos, len) è sinonimo di SUBSTRING (str, pos, len).

OTTOBRE (N)

Restituisce una rappresentazione di stringa del valore ottale di N, dove N è un numero lungo (BIGINT). Ciò è equivalente a CONV (N, 10,8). Restituisce NULL se N è NULL.

SQL> SELECT OCT(12);
+---------------------------------------------------------+
| OCT(12)                                                 |
+---------------------------------------------------------+
| 14                                                      |
+---------------------------------------------------------+
1 row in set (0.00 sec)

OCTET_LENGTH (str)

OCTET_LENGTH () è un sinonimo di LENGTH ().

ORD (str)

Se il carattere più a sinistra della stringa str è un carattere multibyte, restituisce il codice per quel carattere, calcolato dai valori numerici dei suoi byte costituenti usando questa formula -

(1st byte code)
+ (2nd byte code . 256)
+ (3rd byte code . 2562) ...

Se il carattere più a sinistra non è un carattere multibyte, ORD () restituisce lo stesso valore della funzione ASCII ().

SQL> SELECT ORD('2');
+---------------------------------------------------------+
| ORD('2')                                                |
+---------------------------------------------------------+
| 50                                                      |
+---------------------------------------------------------+
1 row in set (0.00 sec)

POSITION (substr IN str)

POSITION (substr IN str) è un sinonimo di LOCATE (substr, str).

QUOTE (str)

Indica una stringa per produrre un risultato che può essere utilizzato come valore di dati con escape appropriato in un'istruzione SQL. La stringa viene restituita racchiusa tra virgolette singole e con ogni istanza di virgolette singole ('), barra rovesciata (' \ '), ASCII NUL e Control-Z preceduta da una barra rovesciata. Se l'argomento è NULL, il valore restituito è la parola "NULL" senza racchiudere virgolette singole.

SQL> SELECT QUOTE('Don\'t!');
+---------------------------------------------------------+
| QUOTE('Don\'t!')                                        |
+---------------------------------------------------------+
| 'Don\'t!'                                               |
+---------------------------------------------------------+
1 row in set (0.00 sec)

NOTE - Per favore controlla se la tua installazione ha qualche bug con questa funzione, quindi non usare questa funzione.

expr modello REGEXP

Questa funzione esegue un pattern match di expr contro pattern. Restituisce 1 se expr corrisponde a pat; altrimenti restituisce 0. Se espr o pat è NULL, il risultato è NULL. REGEXP non distingue tra maiuscole e minuscole, tranne quando viene utilizzato con stringhe binarie.

SQL> SELECT 'ABCDEF' REGEXP 'A%C%%';
+---------------------------------------------------------+
| 'ABCDEF' REGEXP 'A%C%%'                                 |
+---------------------------------------------------------+
| 0                                                       |
+---------------------------------------------------------+
1 row in set (0.00 sec)

Un altro esempio è:

SQL> SELECT 'ABCDE' REGEXP '.*';
+---------------------------------------------------------+
|  'ABCDE' REGEXP '.*'                                    |
+---------------------------------------------------------+
| 1                                                       |
+---------------------------------------------------------+
1 row in set (0.00 sec)

Vediamo un altro esempio:

SQL> SELECT 'new*\n*line' REGEXP 'new\\*.\\*line';
+---------------------------------------------------------+
| 'new*\n*line' REGEXP 'new\\*.\\*line'                   |
+---------------------------------------------------------+
| 1                                                       |
+---------------------------------------------------------+
1 row in set (0.00 sec)

REPEAT (str, count)

Restituisce una stringa composta dalla stringa str ripetuta il conteggio delle volte. Se count è minore di 1, restituisce una stringa vuota. Restituisce NULL se str o count sono NULL.

SQL> SELECT REPEAT('SQL', 3);
+---------------------------------------------------------+
| REPEAT('SQL', 3)                                      |
+---------------------------------------------------------+
| SQLSQLSQL                                         |
+---------------------------------------------------------+
1 row in set (0.00 sec)

SOSTITUISCI (str, from_str, to_str)

Restituisce la stringa str con tutte le occorrenze della stringa from_str sostituite dalla stringa to_str. REPLACE () esegue una corrispondenza con distinzione tra maiuscole e minuscole durante la ricerca di from_str.

SQL> SELECT REPLACE('www.mysql.com', 'w', 'Ww');
+---------------------------------------------------------+
| REPLACE('www.mysql.com', 'w', 'Ww')                     |
+---------------------------------------------------------+
| WwWwWw.mysql.com                                        |
+---------------------------------------------------------+
1 row in set (0.00 sec)

REVERSE (str)

Restituisce la stringa str con l'ordine dei caratteri invertito.

SQL> SELECT REVERSE('abcd');
+---------------------------------------------------------+
| REVERSE('abcd')                                         |
+---------------------------------------------------------+
| dcba                                                    |
+---------------------------------------------------------+
1 row in set (0.00 sec)

DESTRA (str, len)

Restituisce i caratteri len più a destra dalla stringa str o NULL se un argomento è NULL.

SQL> SELECT RIGHT('foobarbar', 4);
+---------------------------------------------------------+
| RIGHT('foobarbar', 4)                                   |
+---------------------------------------------------------+
| rbar                                                    |
+---------------------------------------------------------+
1 row in set (0.00 sec)

RPAD (str, len, padstr)

Restituisce la stringa str, riempita a destra con la stringa padstr a una lunghezza di caratteri len. Se str è più lungo di len, il valore restituito viene abbreviato in caratteri len.

SQL> SELECT RPAD('hi',5,'?');
+---------------------------------------------------------+
| RPAD('hi',5,'?')                                        |
+---------------------------------------------------------+
| hi???                                                   |
+---------------------------------------------------------+
1 row in set (0.00 sec)

RTRIM (str)

Restituisce la stringa str con i caratteri spazio finali rimossi.

SQL> SELECT RTRIM('barbar   ');
+---------------------------------------------------------+
| RTRIM('barbar   ')                                      |
+---------------------------------------------------------+
| barbar                                                  |
+---------------------------------------------------------+
1 row in set (0.00 sec)

SOUNDEX (str)

Restituisce una stringa soundex da str. Due corde che suonano quasi allo stesso modo dovrebbero avere corde soundex identiche. Una stringa soundex standard è lunga quattro caratteri, ma la funzione SOUNDEX () restituisce una stringa arbitrariamente lunga. Puoi usare SUBSTRING () sul risultato per ottenere una stringa soundex standard. Tutti i caratteri non alfabetici in str vengono ignorati. Tutti i caratteri alfabetici internazionali al di fuori dell'intervallo AZ vengono trattati come vocali.

SQL> SELECT SOUNDEX('Hello');
+---------------------------------------------------------+
| SOUNDEX('Hello')                                        |
+---------------------------------------------------------+
| H400                                                    |
+---------------------------------------------------------+
1 row in set (0.00 sec)

expr1 SUONA COME expr2

È lo stesso di SOUNDEX (expr1) = SOUNDEX (expr2).

SPAZIO (N)

Restituisce una stringa composta da N caratteri di spazio.

SQL> SELECT SPACE(6);
+---------------------------------------------------------+
| SELECT SPACE(6)                                         |
+---------------------------------------------------------+
| '      '                                                |
+---------------------------------------------------------+
1 row in set (0.00 sec)

STRCMP (str1, str2)

Confronta due stringhe e restituisce 0 se entrambe le stringhe sono uguali, restituisce -1 se il primo argomento è più piccolo del secondo in base all'ordinamento corrente, altrimenti restituisce 1.

SQL> SELECT STRCMP('MOHD', 'MOHD');
+---------------------------------------------------------+
| STRCMP('MOHD', 'MOHD')                                  |
+---------------------------------------------------------+
| 0                                                       |
+---------------------------------------------------------+
1 row in set (0.00 sec)

Un altro esempio è:

SQL> SELECT STRCMP('AMOHD', 'MOHD');
+---------------------------------------------------------+
| STRCMP('AMOHD', 'MOHD')                                 |
+---------------------------------------------------------+
| -1                                                      |
+---------------------------------------------------------+
1 row in set (0.00 sec)

Vediamo un altro esempio:

SQL> SELECT STRCMP('MOHD', 'AMOHD');
+---------------------------------------------------------+
| STRCMP('MOHD', 'AMOHD')                                 |
+---------------------------------------------------------+
| 1                                                       |
+---------------------------------------------------------+
1 row in set (0.00 sec)

SUBSTRING (str, pos)

SUBSTRING (str FROM pos)

SUBSTRING (str, pos, len)

SUBSTRING (str DA pos PER len)

Le forme senza un argomento len restituiscono una sottostringa dalla stringa str a partire dalla posizione pos. Le forme con un argomento len restituiscono una sottostringa len caratteri lunghi dalla stringa str, a partire dalla posizione pos. I moduli che utilizzano FROM sono la sintassi SQL standard. È anche possibile utilizzare un valore negativo per pos. In questo caso, l'inizio della sottostringa è costituito da caratteri pos dalla fine della stringa, anziché dall'inizio. Un valore negativo può essere utilizzato per pos in una qualsiasi delle forme di questa funzione.

SQL> SELECT SUBSTRING('Quadratically',5);
+---------------------------------------------------------+
| SSUBSTRING('Quadratically',5)                           |
+---------------------------------------------------------+
| ratically                                               |
+---------------------------------------------------------+
1 row in set (0.00 sec)

SQL> SELECT SUBSTRING('foobarbar' FROM 4);
+---------------------------------------------------------+
| SUBSTRING('foobarbar' FROM 4)                           |
+---------------------------------------------------------+
| barbar                                                  |
+---------------------------------------------------------+
1 row in set (0.00 sec)

SQL> SELECT SUBSTRING('Quadratically',5,6);
+---------------------------------------------------------+
| SUBSTRING('Quadratically',5,6)                          |
+---------------------------------------------------------+
| ratica                                                  |
+---------------------------------------------------------+
1 row in set (0.00 sec)

SUBSTRING_INDEX (str, delim, count)

Restituisce la sottostringa dalla stringa str prima del conteggio delle occorrenze del delimitatore delimitato. Se il conteggio è positivo, viene restituito tutto ciò a sinistra del delimitatore finale (contando da sinistra). Se count è negativo, viene restituito tutto ciò a destra del delimitatore finale (contando da destra). SUBSTRING_INDEX () esegue una corrispondenza con distinzione tra maiuscole e minuscole durante la ricerca di delim.

SQL> SELECT SUBSTRING_INDEX('www.mysql.com', '.', 2);
+---------------------------------------------------------+
| SUBSTRING_INDEX('www.mysql.com', '.', 2)                |
+---------------------------------------------------------+
| www.mysql                                               |
+---------------------------------------------------------+
1 row in set (0.00 sec)

TRIM ([{BOTH | LEADING | TRAILING} [remstr] FROM] str)

TRIM ([remstr FROM] str)

Restituisce la stringa str con tutti i prefissi o suffissi remstr rimossi. Se non viene fornito nessuno degli specificatori BOTH, LEADING o TRAILING, si presume ENTRAMBI. remstr è facoltativo e, se non specificato, gli spazi vengono rimossi.

SQL> SELECT TRIM('  bar   ');
+---------------------------------------------------------+
| TRIM('  bar   ')                                        |
+---------------------------------------------------------+
| bar                                                     |
+---------------------------------------------------------+
1 row in set (0.00 sec)

SQL> SELECT TRIM(LEADING 'x' FROM 'xxxbarxxx');
+---------------------------------------------------------+
| TRIM(LEADING 'x' FROM 'xxxbarxxx')                      |
+---------------------------------------------------------+
| barxxx                                                  |
+---------------------------------------------------------+
1 row in set (0.00 sec)

SQL> SELECT TRIM(BOTH 'x' FROM 'xxxbarxxx');
+---------------------------------------------------------+
| TRIM(BOTH 'x' FROM 'xxxbarxxx')                         |
+---------------------------------------------------------+
| bar                                                     |
+---------------------------------------------------------+
1 row in set (0.00 sec)

SQL> SELECT TRIM(TRAILING 'xyz' FROM 'barxxyz');
+---------------------------------------------------------+
| TRIM(TRAILING 'xyz' FROM 'barxxyz')                     |
+---------------------------------------------------------+
| barx                                                    |
+---------------------------------------------------------+
1 row in set (0.00 sec)

UCASE (str)

UCASE () è un sinonimo di UPPER ().

UNHEX (str)

Esegue l'operazione inversa di HEX (str). Cioè, interpreta ogni coppia di cifre esadecimali nell'argomento come un numero e lo converte nel carattere rappresentato dal numero. I caratteri risultanti vengono restituiti come stringa binaria.

SQL> SELECT UNHEX('4D7953514C');
+---------------------------------------------------------+
| UNHEX('4D7953514C')                                     |
+---------------------------------------------------------+
| SQL                                                   |
+---------------------------------------------------------+
1 row in set (0.00 sec)

I caratteri nella stringa dell'argomento devono essere cifre esadecimali consentite: "0" .. "9", "A" .. "F", "a" .. "f". Se UNHEX () rileva cifre non esadecimali nell'argomento, restituisce NULL.

SUPERIORE (str)

Restituisce la stringa str con tutti i caratteri modificati in maiuscolo in base alla mappatura del set di caratteri corrente.

SQL> SELECT UPPER('Allah-hus-samad');
+---------------------------------------------------------+
| UPPER('Allah-hus-samad')                                |
+---------------------------------------------------------+
| ALLAH-HUS-SAMAD                                         |
+---------------------------------------------------------+
1 row in set (0.00 sec)
sql-funzioni-utili.htm