This function takes a decimal value and returns fractional representation of this value.
SELECT dbo.DecimalToFraction(12.5)
Result: 12 1/2
The interesting thing about this code is that it uses a tally table to find the smallest denominator for the decimal part.