KarimS
KarimS

Reputation: 3902

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.

Upvotes: 5

Views: 24682

Answers (3)

TVC
TVC

Reputation: 77

select convert(text_column, character)

Upvotes: 0

Dhruv Saxena
Dhruv Saxena

Reputation: 1346

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

Upvotes: 2

Bernd Buffen
Bernd Buffen

Reputation: 15057

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_ https://mariadb.com/kb/en/mariadb/convert/

Upvotes: 6

Related Questions