I already used this method, but now I have to use some colors depending on the values. So, I have the following information in a table:
Material | Q1 | Q2
---------+----+-----
M1 | 10 | 5
M2 | 3 | 10
M3 | 15 | 15
When the Q1 is lower than Q2 I want to color red, when is high in blue and yellow when is the same value.
CREATE TABLE #tempo
(
q1 INT, q2 INT, name VARCHAR(10)
)
INSERT INTO #tempo (q1, q2, name)
VALUES (10, 5, 'low'), (10, 10, 'same'), (10, 20, 'high')
--SELECT * FROM #tempo
DECLARE @html varchar(MAX)
SET @html = '<table id="tablaPrincipal" border=0>
<tr style="background:#a7bfde;font-weight:bold;">
<td>q1</td>
<td>q2</td>
<td>Compare</td>
</tr>'+
(
SELECT
isnull(q1,0) AS td
,' ' , isnull(q2,0) AS td
,' ' , name AS td
FROM #tempo
FOR XML PATH('tr')
)
+'</TABLE>'
SELECT @html
DROP TABLE #tempo
I tried to use td with another tag tdx and replace like this: SET @html = REPLACE(@html, '', '')
but, it is possible to change the alias dynamically depending on the value?
Thanks!