Wednesday, December 5, 2007

MySQL convert or cast int to string

Suppose you have a table with 2 columns one int and other varchar. Now if you want to update varchar fields in all the records with the int value, use this query :

update table_name set varchar_Field = CAST(int_Field AS CHAR);

If you want to some text in addition to the int field, then use :

update table_name set varchar_Field = CONCAT(CAST(int_Field AS CHAR), 'sampleText');

7 comments:

Unknown said...

+1 :)

Abdullah said...

thanks for this helpful post..

Leosharp said...

Thanks much :D

Anonymous said...

Wat about converting a string to an integer?

Sanjeev said...
This comment has been removed by the author.
Sanjeev said...

how to convert text column to number. say for i want to get max of a varchar column which actually contains numbers as values.

me said...

Just wanted to say thanks for this tip. I was trying to use the REPLACE function on an INT field. It worked in HeidiSQL, a MySQL frontend app. But, I got errors when I ran the same statement using VBA. Thanks to your post, it worked. Again, thanks!