I want to implement unit-conversion functionality in a database for units of mass, volume, temperature, etc. My background is in software development, so I naturally lean towards creating a bunch of User-Defined Functions for this task. But I have sneaking suspicion there is a more “SQLish” way of accomplishing this.
The table-based method, such as the one described here or here would be excellent, but I see no simple way of extending this approach for units which are not related by a single multiplier, such as temperature (F-to-C, C-to-F) or atomic mass (kgmol-to-kg). I have seen multi-step approaches (such as the one described here), but these seem far too convoluted to be useful.
Am I missing something obvious here, or are functions really the only way to go?
In the end, I decided to stick with UDFs. The primary motivation for that decision was that the complexity of the table-based approach didn’t seem to be justified, given the infrequency of this task. Also, several of the functions (e.g.
ConvertMass()) are dependent on subqueries (to determine the atomic weight of a given component, for example). So UDFs seemed like the most prudent way to go.