how to convert varchar into integer type in sql

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • shashikala
    New Member
    • Aug 2013
    • 12

    how to convert varchar into integer type in sql

    Hi all,
    i have two database enteries: 9shashi and 10shashi. both are of varchar type. how should i get maximum integer from these two values i.e,10.
    If i use the query select max(id) from tablename; i am getting 9shashi. But i want to retrieve 10shashi out of there.
    Please help me.
    For help, i am using oracle 10g database express edition and not 'mysql'.
  • buy domain
    New Member
    • Aug 2013
    • 6

    #2
    Code:
    Select Cast([Column1] as INT)

    Comment

    • rski
      Recognized Expert Contributor
      • Dec 2006
      • 700

      #3
      This is because for strings, max is taken in a lexicographical order.
      You have to remove non-number characters and use to_number
      Code:
      select max(to_number(regexp_replace(column_name,'[[:alpha:]]', ''))) from test;

      Comment

      • shashikala
        New Member
        • Aug 2013
        • 12

        #4
        thankyou both for answering. :) well rski method works smoothly. but i have another question, what does [[:alpha:]] does in the query?

        Comment

        • debasisdas
          Recognized Expert Expert
          • Dec 2006
          • 8119

          #5
          For that you need to understand Regular Expression.

          Comment

          • rski
            Recognized Expert Contributor
            • Dec 2006
            • 700

            #6
            See here

            Comment

            Working...