CREATE FUNCTION dbo.udf_HexToRGB (@hex char(6))
RETURNS TABLE
AS RETURN
    (
     SELECT 16 * (CHARINDEX(SUBSTRING(@hex, 1, 1), '0123456789abcdef') - 1) + (CHARINDEX(SUBSTRING(@hex, 2, 1),
                                                                                         '0123456789abcdef') - 1) AS R
           ,16 * (CHARINDEX(SUBSTRING(@hex, 3, 1), '0123456789abcdef') - 1) + (CHARINDEX(SUBSTRING(@hex, 4, 1),
                                                                                         '0123456789abcdef') - 1) AS G
           ,16 * (CHARINDEX(SUBSTRING(@hex, 5, 1), '0123456789abcdef') - 1) + (CHARINDEX(SUBSTRING(@hex, 6, 1),
                                                                                         '0123456789abcdef') - 1) AS B
    )