類型 | 備注 |
---|---|
DATE | YYYY-MM-DD |
DATETIME | YYYY-MM-DD HH:mm:ss |
TIME | HH:mm:ss |
DECIMAL | 通常用于帶小數(shù)位 |
CHAR | 固定長度字符串 |
NCHAR | 類型于CHAR一致 |
SIGNED | 一個(gè)有符號(hào)的64整數(shù)位 |
UNSIGNED | 一個(gè)無符號(hào)的64整數(shù)位 |
BINARY | 二進(jìn)制字符串 |
JSON | MySQL 5.7.8 及更高版本 |
注意:
其中DATE支持的范圍是: 1000-01-01 至 9999-12-31,(實(shí)驗(yàn)版本為:)
如果為: 999-01-01 結(jié)果則會(huì)0999-01-01。
如果為: 01-01-01則會(huì)為:2001-01-01。
mysql> select cast('999-11-11' as DATE); +---------------------------+ | cast('999-11-11' as DATE) | +---------------------------+ | 0999-11-11 | +---------------------------+ 1 row in set (0.00 sec)
mysql> select cast('01-11-11' as DATE); +--------------------------+ | cast('01-11-11' as DATE) | +--------------------------+ | 2001-11-11 | +--------------------------+ 1 row in set (0.00 sec)
mysql> select version(); +-----------+ | version() | +-----------+ | 5.7.20 | +-----------+ 1 row in set (0.00 sec)
2. CAST函數(shù)中expr的值可以轉(zhuǎn)換為type,其轉(zhuǎn)換的結(jié)果才正確,否則轉(zhuǎn)換后的結(jié)果默認(rèn)值,如Null,0等。
例如:一個(gè)Char類型轉(zhuǎn)換為Demical類型,轉(zhuǎn)換的結(jié)果就為0。
mysql> SELECT CAST('ANDYQIAN' AS DECIMAL); +-----------------------------+ | CAST('ANDYQIAN' AS DECIMAL) | +-----------------------------+ | 0 | +-----------------------------+ 1 row in set, 1 warning (0.00 sec)
轉(zhuǎn)換案例
這里給出一些常用的類型轉(zhuǎn)換例子。
DATE 類型
mysql> select cast('2017-12-14' as DATE); +----------------------------+ | cast('2017-12-14' as DATE) | +----------------------------+ | 2017-12-14 | +----------------------------+ 1 row in set (0.00 sec)
TIME 類型
mysql> select cast('12:00:00' as TIME); +--------------------------+ | cast('12:00:00' as TIME) | +--------------------------+ | 12:00:00 | +--------------------------+ 1 row in set (0.00 sec)
DATETIM 類型
mysql> select cast('2017-12-14 00:11:11' as DATETIME); +-----------------------------------------+ | cast('2017-12-14 00:11:11' as DATETIME) | +-----------------------------------------+ | 2017-12-14 00:11:11 | +-----------------------------------------+ 1 row in set (0.00 sec)
SIGNED類型
mysql> select cast('-1024' as SIGNED); +-------------------------+ | cast('-1024' as SIGNED) | +-------------------------+ | -1024 | +-------------------------+ 1 row in set (0.00 sec)
UNSIGNED類型
mysql> select cast('-1024' as UNSIGNED); +---------------------------+ | cast('-1024' as UNSIGNED) | +---------------------------+ | 18446744073709550592 | +---------------------------+ 1 row in set, 1 warning (0.00 sec)
DECIMAL類型
mysql> select cast('18.11' as DECIMAL(18,2)); +--------------------------------+ | cast('18.11' as DECIMAL(18,2)) | +--------------------------------+ | 18.11 | +--------------------------------+ 1 row in set (0.00 sec)
以上就是本次整理的全部內(nèi)容,大家測試的時(shí)候可以輸入命令嘗試下,如果還有任何不明白的地方,大家可以在下方的留言區(qū)域討論。
標(biāo)簽:平頂山 山南 安康 那曲 安徽 浙江 四川 濮陽
巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《淺析MySQL顯式類型轉(zhuǎn)換》,本文關(guān)鍵詞 淺析,MySQL,顯式,類型,轉(zhuǎn)換,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。