question

eghetto avatar image
eghetto asked

How to create HTML color codes with SQL?

Dear all, I would like to create unique HTML color codes (like "#CC9900" used in a SSRS report) for my databases: First (poor) attempt: SELECT databases.name ,SUBSTRING(HASHBYTES('SHA1', databases.name), 1, 3) Result FROM sys.databases Better idea? Thanks!
html
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

eghetto avatar image
eghetto answered
Figured out by myself now: ALTER FUNCTION [dbo].[HtmlColorCode] ( @String NVARCHAR(MAX) ) RETURNS TABLE AS RETURN ( SELECT '#' + UPPER(RIGHT(master.dbo.fn_varbintohexstr(SUBSTRING(HASHBYTES('SHA1', @String), 1, 3)), 6)) AS HtmlColorCode ) Like it?
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

tomgough79 avatar image
tomgough79 answered
It looks like your existing idea is reasonable. To be honest, the main pitfall I can see in your method here is that you may get some very similar colours coming out. But this is always likely to be a risk if you dynamically generate the colours from data in sys.databases The only other thought I could offer is to create a static table somewhere with columns for database name and colour code and manage it manually
1 comment
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

eghetto avatar image eghetto commented ·
A static colors table is not very "sexy" :) Thanks for your comment though!
1 Like 1 ·

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.