How to retrieve float values from data base

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sijayreddy
    New Member
    • Dec 2007
    • 4

    How to retrieve float values from data base

    Hi all
    For ex am having the table student with fields student name and marks...Where as name is of the type string and marks float value....and having the records
    Studentname Marks
    aaa 10.5
    bbb 15.3
    Now am writing the query [CODE=mysql]Select * from table where Marks=10.5;[/CODE]
    But while executing this query am getting 0 results....What can i do...Is there any way to solve this problem....Othe r than '=' operator for all (>,<,like,In)a m getting the result...Please suggest me
    Last edited by mwasif; Dec 11 '07, 03:02 PM. Reason: Added [CODE=mysql]/[/CODE] tags
  • amitpatel66
    Recognized Expert Top Contributor
    • Mar 2007
    • 2358

    #2
    Originally posted by sijayreddy
    Hi all
    For ex am having the table student with fields student name and marks...Where as name is of the type string and marks float value....and having the records
    Studentname Marks
    aaa 10.5
    bbb 15.3
    Now am writing the query Select * from table where Marks=10.5;
    But while executing this query am getting 0 results....What can i do...Is there any way to solve this problem....Othe r than '=' operator for all (>,<,like,In)a m getting the result...Please suggest me
    try this:

    [code=sql]

    SELECT * from table WHERE ROUND(marks) = ROUND(10.5);

    [/code]

    Comment

    • sijayreddy
      New Member
      • Dec 2007
      • 4

      #3
      Originally posted by amitpatel66
      try this:

      [code=sql]

      SELECT * from table WHERE ROUND(marks) = ROUND(10.5);

      [/code]
      i think this may not be correct ...Why because we are using the round function means we are making that Float value as integer...I don't want to compare two integers i want to compare float values...becaus e in my application even 0.4 value makes a lot difference...pl ease tell me the reason for this and what to do...

      Comment

      • mwasif
        Recognized Expert Contributor
        • Jul 2006
        • 802

        #4
        Hi sijayreddy,

        Welcome to TSDN!!!

        Kindly use CODE tags when posting source code.

        What is the data type of 'Marks'? I know you have told above, but I want to make sure. There is no problem with your query. Or you can try this
        [CODE=mysql]SELECT * FROM table WHERE Marks>=10.5 AND Marks<=10.5;[/CODE]

        Comment

        • sijayreddy
          New Member
          • Dec 2007
          • 4

          #5
          Originally posted by mwasif
          Hi sijayreddy,

          Welcome to TSDN!!!

          Kindly use CODE tags when posting source code.

          What is the data type of 'Marks'? I know you have told above, but I want to make sure. There is no problem with your query. Or you can try this
          [CODE=mysql]SELECT * FROM table WHERE Marks>=10.5 AND Marks<=10.5;[/CODE]
          Hi
          Hear am giving all the details how i created the table and inserted the data and the query for retrieving the data
          [CODE=mysql]
          create table ex(name char,mark float,total float);
          insert into ex values ('a',10.5,55.5) ;
          insert into ex values ('b',1156.66,56 56.65);
          select * from ex where mark=10.5;
          select * from ex where mark=1156.66 ;[/CODE]

          for the first query i.e Select * from ex where mark=10.5... getting the correct result but for the second select query select * from ex where mark=1156.66 query browser is returning ZERO result....
          Kindly tell me the reason what is the reason for this and how to resolve the problem

          Comment

          • mwasif
            Recognized Expert Contributor
            • Jul 2006
            • 802

            #6
            Change data type to DOUBLE using the below query
            [CODE=mysql]ALTER TABLE `ex` CHANGE `mark` `mark` DOUBLE( 14, 2 ) NULL DEFAULT NULL[/CODE]
            Now test your query
            [CODE=mysql]SELECT * FROM ex WHERE mark=1156.66 ;[/CODE]
            FLOAT values are not stored as exact values inside computer architecture.

            Comment

            • sijayreddy
              New Member
              • Dec 2007
              • 4

              #7
              Originally posted by mwasif
              Change data type to DOUBLE using the below query
              [CODE=mysql]ALTER TABLE `ex` CHANGE `mark` `mark` DOUBLE( 14, 2 ) NULL DEFAULT NULL[/CODE]
              Now test your query
              [CODE=mysql]SELECT * FROM ex WHERE mark=1156.66 ;[/CODE]
              FLOAT values are not stored as exact values inside computer architecture.
              Thank u mwasif...it is working now...

              Comment

              • mwasif
                Recognized Expert Contributor
                • Jul 2006
                • 802

                #8
                You are welcome. Postback whenever you need help.

                Comment

                Working...