Monday, January 6, 2014

Cast string to integer in mysql

So you want to convert an attribute with string (varchar) type to a number (integer) in your mysql query ?
If you have enough time have a look here, otherwise just blindly follow the following example.
  Suppose you have some code 002, 007, 001 that you want to be 2, 7, 1.
SELECT  CAST(CODE AS UNSIGNED) FROM MY_TABLE;
 The same scenerio may be repeated with other types, it that case
  the type for the result can be one of the following values:
  • BINARY[(N)]
  • CHAR[(N)]
  • DATE
  • DATETIME
  • DECIMAL[(M[,D])]
  • SIGNED [INTEGER]
  • TIME
  • UNSIGNED [INTEGER]
 So far so good, isn't it ?

No comments:

Post a Comment