Articles

SQL – String Functions (Italiano)

Posted by admin
Advertisements

SQL string functions are used primarily for string manipulation. The following table details the important string functions −

Sr.No., SUBSTR()

Returns the substring as specified

46 TRIM()

Removes leading and trailing spaces

47 UCASE()

Synonym for UPPER()

48 UNHEX()

Converts each pair of hexadecimal digits to a character

49 UPPER()

Converts to uppercase

ASCII(str)

Returns the numeric value of the leftmost character of the string str., Restituisce 0 se str è la stringa vuota. Restituisce NULL se str è NULL. ASCII () funziona per i caratteri con valori numerici da 0 a 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 stringa del valore binario di N, dove N è un numero longlong (BIGINT). Questo è 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)

CARATTERE (N,…, )

CHAR () interpreta ogni argomento N come un numero intero e restituisce una stringa composta dai caratteri dati dai valori di codice di quei 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 multi-byte 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 risultante dalla concatenazione degli argomenti. Può avere uno o più argomenti. Se tutti gli argomenti sono stringhe non binarie, il risultato è una stringa non binaria. Se gli argomenti includono qualsiasi stringa binaria, 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(separator,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 stringa del numero N, convertito da base from_base a to_base. Restituisce NULL se qualsiasi 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 come un numero firmato. In caso contrario, N viene trattato come non firmato. CONV () funziona con 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(bits,on,off])

Restituisce una stringa tale che per ogni bit impostato nei bit di valore, 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 alto). Le stringhe vengono aggiunte al risultato da sinistra a destra, separate dalla stringa separatore (il valore predefinito è 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 in str1, str2, str3,… elenco. 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 nella lista delle stringhe strlist composta 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)

FORMAT(X,D)

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

HEX(N_or_S)

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

Se N_or_S è una stringa, restituisce una rappresentazione stringa esadecimale di N_or_S in cui 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 dalla posizione pos e i caratteri len lunghi sostituiti dalla stringa newstr. Restituisce la stringa originale se pos non è all’interno della lunghezza della stringa. Sostituisce il resto della stringa dalla posizione pos se len non è all’interno della lunghezza del resto della stringa. Restituisce NULL se qualsiasi 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. Questo è lo stesso della 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().

LEFT(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)

LENGTH(str)

Restituisce la lunghezza della stringa str, misurata in byte., Un carattere multi-byte 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(file_name)

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 e disporre del privilegio FILE. Il file deve essere leggibile da tutti e la sua dimensione inferiore ai byte max_allowed_packet.,

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)

LOWER(str)

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

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 impostati. str1 corrisponde al bit 0, str2 al bit 1 e così via. 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) è un sinonimo di SOTTOSTRINGA(str,pos,len).

OCT(N)

Restituisce una rappresentazione stringa del valore ottale di N, dove N è un numero longlong (BIGINT)., Questo è 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 primo carattere a sinistra della stringa str è un multi-byte character, restituisce il codice per il personaggio, calcolato sulla base dei valori numerici della sua componente di byte utilizzando questa formula −

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

Se il primo carattere a sinistra non è un multi-byte character, ORD() restituisce lo stesso valore ASCII() funzione.,

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)

Cita una stringa per produrre un risultato che può essere utilizzato come valore di dati correttamente escape in un’istruzione SQL. La stringa viene restituita racchiusa da 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)

NOTA-Si prega di verificare se l’installazione ha qualche bug con questa funzione, quindi non utilizzare questa funzione.

expr REGEXP pattern

Questa funzione esegue una corrispondenza di pattern di expr contro pattern. Restituisce 1 se expr corrisponde a pat; altrimenti restituisce 0. Se expr o pat è NULL, il risultato è NULL. REGEXP non è case sensitive, 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)

RIPETERE(str,conte)

Restituisce una stringa contenente la stringa str ripetuto conte volte. Se il conteggio è inferiore a 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)

REPLACE(str,from_str,to_str)

Restituisce la stringa str con tutte le occorrenze della stringa from_str sostituita dalla stringa to_str. REPLACE () esegue una corrispondenza sensibile al maiuscolo / minuscolo 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)

RIGHT(str,len)

Restituisce i caratteri len più a destra della 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, imbottita 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 di 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 stringhe che suonano quasi uguali dovrebbero avere stringhe soundex identiche. Una stringa soundex standard è lunga quattro caratteri, ma la funzione SOUNDEX () restituisce una stringa arbitrariamente lunga. È possibile utilizzare 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 della gamma AZ sono trattati come vocali.

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

expr1 SUONA COME expr2

Questo è 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 DA pos)

SUBSTRING(str DA pos PER len)

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

SUBSTRING_INDEX(str,delim,count)

Restituisce la sottostringa dalla stringa str prima delle occorrenze di conteggio del delimitatore delim. Se il conteggio è positivo, viene restituito tutto a sinistra del delimitatore finale (contando da sinistra). Se il conteggio è negativo, viene restituito tutto a destra del delimitatore finale (contando da destra)., SUBSTRING_INDEX () esegue una corrispondenza sensibile al maiuscolo / minuscolo 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( FROM] str)

Restituisce la stringa str con tutti i prefissi o suffissi remstr rimossi. Se non viene fornito nessuno degli specificatori ENTRAMBI, INIZIALI o FINALI, ENTRAMBI vengono assunti. remstr è facoltativo e, se non specificato, gli spazi vengono rimossi.

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 una stringa binaria.

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

UPPER(str)

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

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

Leave A Comment