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.

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)

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like