'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 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.



Solution 1:[1]

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

Solution 2:[2]

select convert(text_column, character)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Dhruv Saxena
Solution 2 TVC