Formating color in SQL Server using FOR XML PATH

前端 未结 3 1378
时光说笑
时光说笑 2021-01-24 23:48

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
------         


        
3条回答
  •  忘掉有多难
    2021-01-25 00:18

    Here is another option. Not clear if you wanted just the one cell highlighted

    EDIT - Updated for 3 colors

    Example

    DECLARE @html varchar(MAX)
    SET @html = ''+
                        (
                        SELECT [td/@style] = 'background:'+choose(sign(q1-q2)+2,'blue;color:white;','yellow','red')
                              ,[td] = isnull(q1,0)
                              ,null
                              ,[td/@style] = 'background:'+choose(sign(q1-q2)+2,'blue;color:white;','yellow','red')
                              ,[td] = isnull(q2,0) 
                              ,null
                              ,[td/@style] = 'background:'+choose(sign(q1-q2)+2,'blue;color:white;','yellow','red')
                              ,[td] = name 
                         FROM #tempo
                        FOR XML PATH('tr')
                        )
                        +'
    q1 q2 Compare
    ' SELECT @html

    Returns

提交回复
热议问题