Cast Text to Varchar Mysql
I Need to Convert a Text Type to Varchar So Here Is What I Did: Insert into Phpfox. Phpfox_Photo_Album_Info(Album_Id, Description) Select Id, Cast(Description...
i need to convert a text type to varchar so here is what i did :
INSERT INTO phpfox.phpfox_photo_album_info(album_id, description)
SELECT id, CAST(description as varchar(255)) FROM crea8social.photo_album
Its give me this sql error :
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
If i replace the CAST(description as varchar(255)) with "test" its work.
3 Answers
CAST To CHAR. VARCHAR is not supported see:
SELECT CAST("1234567890" as char(5));
sample
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]>
see manual_
Is 255 characters the max length of text that you'd want to receive?
Probably you could look at SUBSTRING() function. In this case, the query would look like:
SELECT id, SUBSTRING(description, 1, 255) AS test FROM crea8social.photo_album
select convert(text_column, character)