How to divide a value into Two

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Vajrala Narendra
    New Member
    • Jun 2007
    • 73

    How to divide a value into Two

    Please give appropriate sql query

    in a table i have a field Field1 of Numeric datatype
    i stored 9.45 in that
    how to separate that value (9.45) in two as 9, 45

    please send appropriate query
    Thank You
  • mwasif
    Recognized Expert Contributor
    • Jul 2006
    • 802

    #2
    Here is a simple example to achieve this
    [CODE=mysql]SELECT SUBSTRING_INDEX (Field1, '.', 1), SUBSTRING_INDEX (Field1, '.', -1)
    FROM table[/CODE]
    Last edited by mwasif; Feb 10 '08, 07:13 AM. Reason: Changed the query

    Comment

    • debasisdas
      Recognized Expert Expert
      • Dec 2006
      • 8119

      #3
      For this the logic is to consider the number as string not number and findout the posistion of the "."

      Comment

      • Vajrala Narendra
        New Member
        • Jun 2007
        • 73

        #4
        Originally posted by mwasif
        Here is a simple example to achieve this
        [CODE=mysql]SELECT SUBSTRING_INDEX (Field1, '.', 1), SUBSTRING_INDEX (Field1, '.', -1)
        FROM table[/CODE]
        Thanks
        i was tried it in sql2005 but it is giving error like 'SUBSTRING_INDE X' is not a recognized function name.
        plz give appropriate query in sql2005

        Comment

        • amitpatel66
          Recognized Expert Top Contributor
          • Mar 2007
          • 2358

          #5
          Originally posted by Vajrala Narendra
          Please give appropriate sql query

          in a table i have a field Field1 of Numeric datatype
          i stored 9.45 in that
          how to separate that value (9.45) in two as 9, 45

          please send appropriate query
          Thank You
          Try this:

          [code=sql]

          SELECT REPLACE('9.45', '.',',') from table_name;

          [/code]

          Comment

          • mwasif
            Recognized Expert Contributor
            • Jul 2006
            • 802

            #6
            Originally posted by Vajrala Narendra
            Thanks
            i was tried it in sql2005 but it is giving error like 'SUBSTRING_INDE X' is not a recognized function name.
            plz give appropriate query in sql2005
            SUBSTRING_INDEX () is a MySQL function.

            Moving the thread to MS SQL Forum.

            Comment

            • ck9663
              Recognized Expert Specialist
              • Jun 2007
              • 2878

              #7
              try:

              Code:
              SELECT REPLACE(cast(Field1 as varchar(20)),'.',',') FROM table_name
              --ck

              Comment

              Working...