SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Evangelist
    Join Date
    Sep 2006
    Posts
    428
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Convert Meters to Feet/Inches in Query?

    Have a situation where I need to show a US and Metric measurement on a web page. The spec in the database is in metric (meters). Of course I can use an equation in php to convert the units but it would be much better (I think?) to go ahead and convert the units in the mysql query.

    I have a very VERY basic understanding of math in queries.

    Maybe I can convert the meters to inches in the query and use php on the page to convert from inches to inches/feet or should I do it all in one swoop in the query?

    Any guidance or push in the right direction would be very helpful.

    This is being done with products specs so sometimes there will only be one product being queried and sometimes there will be a large list of them (50 max prolly) was thinking it would be easier and more efficient to maintain the calculation code in the query rather than having a php function.

    Also this conversion method will be used to convert liters to gallons, cm's to inches...etc The products spec lists have between 10 -15 specs that will all need to be converted on the fly.

    Thanks in advance for any guidance or assistance you may be able to provide.

  2. #2
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,215
    Mentioned
    58 Post(s)
    Tagged
    3 Thread(s)
    i would do this in the front end application

    you ~could~ do it in the query, but you will forever be futzing with the SQL

    most installations (you know, where there are people like DBAs and Operations in charge of the production apps, and developers building new apps) will frown on repeated requests to tinker with the SQL

    in my opinion it's better to have the database interface used strictly for retrieval, and then use the app language to present it whichever way is desired

    same concept goes for date formatting, by the way
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"

  3. #3
    SitePoint Evangelist
    Join Date
    Sep 2006
    Posts
    428
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yup, figured that's where I would be heading. Thanks for the guidance. Built some nice conversion functions and have implemented them post-query to the records. Works very well and will be a breeze to maintain/modify (and also re-use) if I need the conversion anywhere else.

    PHP was definitely the best place to make this happen.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •