CONVERT

Stai visualizzando una vecchia versione di questo article. Visualizza la versione più recente.

Syntassi

CONVERT(espr, tipo), CONVERT(espr USING transcoding_name)

Spiegazione

Le funzioni CONVERT() e CAST() accettano un valore di un tipo e restituiscono un valore di un altro tipo.

Il tipo può essere uno dei seguenti:

  • BINARY[(N)]
  • CHAR[(N)]
  • DATE
  • DATETIME[(D)] Il decimale (D) è stato aggiunto in 5.3
  • DECIMAL[(M[,D])]
  • DOUBLE[(M[,D])] Aggiunto in 5.3
  • INTEGER Aggiunto in 5.3
    • Abbreviazione per SIGNED INTEGER
  • SIGNED [INTEGER]
  • TIME[(D)] Il decimale (D) è stato aggiunto in 5.3
  • UNSIGNED [INTEGER]

Si noti che in MariaDB, INT e INTEGER sono sinonimi.

BINARY produce una stringa del tipo BINARY. Si veda http://dev.mysql.com/doc/refman/5.1/en/binary-varbinary.html per una spiegazione su come questo influenza i confronti tra i valori. Se la lunghezza opzionale N è specificata, BINARY(N) fa sì che la conversioni non utilizzi più di N byte dell'argomento. I valori più brevi di N byte vengono riempiti con byte 0x00 fino a raggiungere la lunghezza N.

CHAR(N) fa sì che la conversioni non utilizzi più di N caratteri dell'argomento.

La differenza principale tra CAST() e CONVERT() è che CONVERT(espr, tipo) è una sintassi ODBC, mentre CAST(espr as tipo) e CONVERT(... USING ...) sono sintassi di SQL92.

CONVERT() with USING is used to convert data between different character sets. In MySQL, transcoding names are the same as the corresponding character set names. For example, this statement converts the string 'abc' in the default character set to the corresponding string in the utf8 character set:

SELECT CONVERT('abc' USING utf8);

Examples:

SELECT enum_col FROM tbl_name ORDER BY CAST(enum_col AS CHAR);

See Also:

Commenti

Sto caricando i commenti......
Content reproduced on this site is the property of its respective owners, and this content is not reviewed in advance by MariaDB. The views, information and opinions expressed by this content do not necessarily represent those of MariaDB or any other party.