Sort, Filter Height In MySQL
Posted by tech on
March 17, 2009
|
|
I came across a situation where I had to filter out a height column so that I will only get rows between, say 5′6 as the minimum. Now, if let us say you are required to filter out all rows with a height between 5′3 and 6′5, you cannot possibly enumerate each height in the IN keyword of SQL. Well, you can, but that is not maximizing the capabilities of programming then. What you can do is extract the values of the height column and get the feet and inches value respectively. With that, you can multiply the foot value by 12 since 1 foot is equal to 12 inches, then add the remaining inches to the resulting product.
Using the MySQL function substring_index(), you can retrieve a substring value at the specified index. So for example, if your height values are in this format 5′6, you can do this in MySQL …
1 | where (substring_index(height, "'", 1) * 12 + substring_index(height, "'", -1)) >= 66 |
The MySQL query above filters out rows that have a minimum height of 5′6.
Found this post useful? Buy me a cup of coffee or help support the sponsors on the right.






