在MySQL中如何进行数据类型的转换?

yecao1227 2006-06-14 05:23:47
在MySQL中如何进行数据类型的转换?如:从float到int类型转换?在日期类型变量后加上一个int类型能否还得到日期类型?
...全文
17980 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
yecao1227 2006-06-16
  • 打赏
  • 举报
回复
谢谢大家的回复,另我又找到一个函数truncate(float,int)可以把float类型转换到int类型,具体语法自己看。
eqxu 2006-06-16
  • 打赏
  • 举报
回复



多谢


经典贴
hy2003fly 2006-06-15
  • 打赏
  • 举报
回复
MySQL的类型转换函数如下:
CAST(expr AS type), CONVERT(expr,type), CONVERT(expr USING transcoding_name)

The CAST() and CONVERT() functions take a value of one type and produce a value of another type.

The type can be one of the following values:

BINARY[(N)]

CHAR[(N)]

DATE

DATETIME

DECIMAL

SIGNED [INTEGER]

TIME

UNSIGNED [INTEGER]


mysql> select cast(now() as char);
+---------------------+
| cast(now() as char) |
+---------------------+
| 2006-06-15 09:31:20 |
+---------------------+


mysql> select convert('中文' using gb2312);
+------------------------------+
| convert('中文' using gb2312) |
+------------------------------+
| 中文 |
+------------------------------+
1 row in set (0.06 sec)

使用日期函数date_add,date_sub就可以了,见下例:
mysql> select date_add(now(),interval 1 day);
+--------------------------------+
| date_add(now(),interval 1 day) |
+--------------------------------+
| 2006-06-16 09:36:06 |
+--------------------------------+
1 row in set (0.02 sec)

mysql> select date_add(now(), interval 1 month);
+-----------------------------------+
| date_add(now(), interval 1 month) |
+-----------------------------------+
| 2006-07-15 09:36:38 |
+-----------------------------------+
1 row in set (0.00 sec)

mysql> select date_add(now(), interval 1 minute);
+------------------------------------+
| date_add(now(), interval 1 minute) |
+------------------------------------+
| 2006-06-15 09:38:03 |
+------------------------------------+
1 row in set (0.00 sec)

1 row in set (0.05 sec)
klan 2006-06-15
  • 打赏
  • 举报
回复
CONVERT() provides a way to convert data between different character sets. The syntax is:

CONVERT(expr USING transcoding_name)

In MySQL, transcoding names are the same as the corresponding character set names.

Examples:

SELECT CONVERT(_latin1'Müller' USING utf8);
INSERT INTO utf8table (utf8column)
SELECT CONVERT(latin1field USING utf8) FROM latin1table;

CONVERT(... USING ...) is implemented according to the standard SQL specification.

You may also use CAST() to convert a string to a different character set. The syntax is:

CAST(character_string AS character_data_type CHARACTER SET charset_name)

Example:

SELECT CAST(_latin1'test' AS CHAR CHARACTER SET utf8);

If you use CAST() without specifying CHARACTER SET, the resulting character set and collation are defined by the character_set_connection and collation_connection system variables. If you use CAST() with CHARACTER SET X, the resulting character set and collation are X and the default collation of X.

You may not use a COLLATE clause inside a CAST(), but you may use it outside. That is, CAST(... COLLATE ...) is illegal, but CAST(...) COLLATE ... is legal.

Example:

SELECT CAST(_latin1'test' AS CHAR CHARACTER SET utf8) COLLATE utf8_bin;

56,677

社区成员

发帖
与我相关
我的任务
社区描述
MySQL相关内容讨论专区
社区管理员
  • MySQL
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧