Take the 2-minute tour ×
Database Administrators Stack Exchange is a question and answer site for database professionals who wish to improve their database skills and learn from others in the community. It's 100% free, no registration required.

From my answer at SO, I have the following function:

def rgb_to_hue(r,g,b)
  # normalize r, g and b
  r_ = r / 255.0
  g_ = g / 255.0
  b_ = b / 255.0

  c_min = [r_,g_,b_].min
  c_max = [r_,g_,b_].max

  delta = (c_max - c_min).to_f

  # compute hue
  hue = 60 * ((g_ - b_)/delta % 6) if c_max == r_
  hue = 60 * ((b_ - r_)/delta + 2) if c_max == g_
  hue = 60 * ((r_ - g_)/delta + 4) if c_max == b_

  return hue

How would the SQL equivalent of this look? And also, is it possible to use it in an ORDER BY clause?

share|improve this question
User-defined functions are not written in SQL, they are written in C. Perhaps you are thinking of stored functions? In any event, you'll need to make some effort, rather than ask us to write your code for you. –  Michael - sqlbot May 1 '14 at 14:19

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.