将文本转换为Varchar mysql

时间:2017-01-28 21:17:28

标签: mysql

我需要将文本类型转换为varchar,所以这就是我所做的:

INSERT INTO phpfox.phpfox_photo_album_info(album_id, description)
SELECT id, CAST(description as varchar(255)) FROM crea8social.photo_album

它给我这个SQL错误:

SQL Error [1064] [42000]: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'varchar(255)) FROM crea8social.photo_albums' at line 2

如果我将CAST(description as varchar(255))替换为"test"其工作。

2 个答案:

答案 0 :(得分:2)

CAST TO CHAR。不支持VARCHAR请参阅:

SELECT CAST("1234567890" as char(5));

<强>样品

MariaDB [test]>   SELECT CAST("1234567890" as char(5));
+-------------------------------+
| CAST("1234567890" as char(5)) |
+-------------------------------+
| 12345                         |
+-------------------------------+
1 row in set, 1 warning (0.00 sec)

MariaDB [test]>

请参阅manual_ https://mariadb.com/kb/en/mariadb/convert/

答案 1 :(得分:1)

您想要接收的文字的最大长度是255个字符吗? 你可能会看SUBSTRING()函数。在这种情况下,查询将如下所示:

SELECT id, SUBSTRING(description, 1, 255) AS test FROM crea8social.photo_album