1

I need to extract an integer in this string. How would I do this using MySQL or PostgreSQL?

user_id='12345'

In this string I must get the number 12345.

2
  • And by "extract from" you actually mean to convert the whole string, not some part of it? Commented Jul 11, 2012 at 17:11
  • Wish I could select both as the answer. Commented Jul 11, 2012 at 21:16

2 Answers 2

10

You would use the CAST function.

SELECT CAST(user_id AS INT) FROM Table

Reference: http://dev.mysql.com/doc/refman/5.5/en/cast-functions.html#function_cast

Sign up to request clarification or add additional context in comments.

Comments

4

Besides the standard SQL syntax demonstrated by @Daniel Li, there is also the PostgreSQL specific shorthand:

user_id::int

The manual will happily tell you about this really basic stuff ...

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.