Let's say that I have a VARCHAR (which contains numeric data) that I want to use for a simple computation (adding 10 to it). According to the MySQL documentation on CAST functions, I could accomplish this with either a CAST or a CONVERT:

SELECT (CAST(field1 AS SIGNED)) + 10
FROM myTable;

or:

SELECT (CONVERT(field1,SIGNED)) + 10
FROM myTable;

What is the difference between CAST and CONVERT in this sense? Are they both really accomplishing the same thing?

up vote 8 down vote accepted

Yes, CAST and CONVERT do the same thing, except that CONVERT allows more options, such as changing character set with USING.

  • 1
    Is there a kind of performance difference o something like that... i meant there's must be a difference.. – user7479 Mar 15 '12 at 22:48
  • 2
    cast is part of the SQL standard, while convert is not – a_horse_with_no_name Mar 1 '17 at 11:54

Convert and cast docs. Aside from the syntax I believe they can functionally be considered synonyms, even when trying to change character sets.

Your Answer

 
discard

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.