我的 SQL 查询中需要 HTML 代码来格式化带有千位分隔符的表格编号,并使总列值以粗体显示

Posted

技术标签:

【中文标题】我的 SQL 查询中需要 HTML 代码来格式化带有千位分隔符的表格编号,并使总列值以粗体显示【英文标题】:HTML codes needed in my SQL query to format table numbers with thousand separator and make Total column values in bold 【发布时间】:2018-02-25 05:56:09 【问题描述】:

我有以下 T-SQL 查询,它输出用于格式化表格的 html 代码。查询和输出都如下所示。

我现在需要添加所需的 HTML 代码来实现这两件事:

    使用千位分隔符分隔数字 用粗体显示最后一列的值(即:总计)

这是 SQL 查询:

declare @tableHtml nvarchar(max)
declare @style nvarchar(50)
declare @MyTable table
(
    StayYear nvarchar(10),
    PropertyCode nvarchar(10),
    Jan nvarchar(10),
    Feb nvarchar(10),
    Mar nvarchar(10),
    Apr nvarchar(10),
    May nvarchar(10),
    Jun nvarchar(10),
    Jul nvarchar(10),
    Aug nvarchar(10),
    Sep nvarchar(10),
    Oct nvarchar(10),
    Nov nvarchar(10),
    Dec nvarchar(10),
    Total nvarchar(50)
)
insert into @MyTable 
SELECT * FROM ITB

select @tableHtml = (

select
    '1' as '@border',
    '4' as '@cellpadding',
    'font-size:10px; font-family:Arial' as '@style',

    (
        select  (select @style as '@style', 'StayYear' as '*' for xml path('th'), type),
                (select @style as '@style', 'PropertyCode' as '*' for xml path('th'), type),
                (select @style as '@style', 'Jan' as '*' for xml path('th'), type),
                (select @style as '@style', 'Feb' as '*' for xml path('th'), type),
                (select @style as '@style', 'Mar' as '*' for xml path('th'), type),
                (select @style as '@style', 'Apr' as '*' for xml path('th'), type),
                (select @style as '@style', 'May' as '*' for xml path('th'), type),
                (select @style as '@style', 'Jun' as '*' for xml path('th'), type),
                (select @style as '@style', 'Jul' as '*' for xml path('th'), type),
                (select @style as '@style', 'Aug' as '*' for xml path('th'), type),
                (select @style as '@style', 'Sep' as '*' for xml path('th'), type),
                (select @style as '@style', 'Oct' as '*' for xml path('th'), type),
                (select @style as '@style', 'Nov' as '*' for xml path('th'), type),
                (select @style as '@style', 'Dec' as '*' for xml path('th'), type),
                (select @style as '@style', 'Total' as '*' for xml path('th'), type)


        for xml path('tr'), type
    ),
    (
        select 'trclass' as '@class',
                (case when [PropertyCode]='A' then 'background-color:#d9dcf0' 
                      when [PropertyCode]='B' then 'background-color:#ebfadc' 
                      when [PropertyCode]='C' then 'background-color:#d9dcf0' 

                 end) as '@style',

                (select StayYear as '*' for xml path('td'), type),
                (select PropertyCode as '*' for xml path('td'), type),
                (select Jan as '*' for xml path('td'), type),
                (select Feb as '*' for xml path('td'), type),
                (select Mar as '*' for xml path('td'), type),
                (select Apr as '*' for xml path('td'), type),
                (select May as '*' for xml path('td'), type),
                (select Jun as '*' for xml path('td'), type),
                (select Jul as '*' for xml path('td'), type),
                (select Aug as '*' for xml path('td'), type),
                (select Sep as '*' for xml path('td'), type),
                (select Oct as '*' for xml path('td'), type),
                (select Nov as '*' for xml path('td'), type),
                (select Dec as '*' for xml path('td'), type),
                (select Total as '*' for xml path('td'), type)


        from @MyTable
        GROUP BY [StayYear], [PropertyCode], [Jan], [Feb], [Mar], [Apr], [May], [Jun], [Jul], [Aug], [Sep], [Oct], [Nov], [Dec], [Total]
        ORDER BY  [PropertyCode], [StayYear] DESC
        for xml path('tr'), type
    )

for xml path('table')
)

select @tableHtml

这是输出(当被视为 HTML 文件时)的外观:

想要的输出如下:

我该怎么做?

【问题讨论】:

请告诉我们SQL server版本和ITB表中列的DataType。 SQL Server 2012。StayYear 和 PropertyCode 列是 nvarchar,列 Jan 到 Total 是 int。 【参考方案1】:

我为千位分隔符添加了Format 函数,并为粗体添加了font-weight 属性。请检查-

 declare @tableHtml nvarchar(max)
 declare @style nvarchar(50)
 declare @FormatStyle nvarchar(10)='#,0.00'
 declare @MyTable table
 (
     StayYear nvarchar(10),
     PropertyCode nvarchar(10),
     Jan nvarchar(10),
     Feb nvarchar(10),
     Mar nvarchar(10),
     Apr nvarchar(10),
     May nvarchar(10),
     Jun nvarchar(10),
     Jul nvarchar(10),
     Aug nvarchar(10),
     Sep nvarchar(10),
     Oct nvarchar(10),
     Nov nvarchar(10),
     Dec nvarchar(10),
     Total nvarchar(50)
 )
 insert into @MyTable ([StayYear],[PropertyCode],[Jan],[Feb],[Mar],[Apr],[May],[Jun],[Jul],[Aug],[Sep],[Oct],[Nov],[Dec],[Total])
 select [StayYear],
        [PropertyCode],
        format([Jan],@FormatStyle),
        format([Feb],@FormatStyle),
        format([Mar],@FormatStyle),
        format([Apr],@FormatStyle),
        format([May],@FormatStyle),
        format([Jun],@FormatStyle),
        format([Jul],@FormatStyle),
        format([Aug],@FormatStyle),
        format([Sep],@FormatStyle),
        format([Oct],@FormatStyle),
        format([Nov],@FormatStyle),
        format([Dec],@FormatStyle),
        format([Total],@FormatStyle)
 from ITB

 select @tableHtml = (

 select
     '1' as '@border',
     '4' as '@cellpadding',
     'font-size:10px; font-family:Arial' as '@style',

     (
         select  (select @style as '@style', 'StayYear' as '*' for xml path('th'), type),
                 (select @style as '@style', 'PropertyCode' as '*' for xml path('th'), type),
                 (select @style as '@style', 'Jan' as '*' for xml path('th'), type),
                 (select @style as '@style', 'Feb' as '*' for xml path('th'), type),
                 (select @style as '@style', 'Mar' as '*' for xml path('th'), type),
                 (select @style as '@style', 'Apr' as '*' for xml path('th'), type),
                 (select @style as '@style', 'May' as '*' for xml path('th'), type),
                 (select @style as '@style', 'Jun' as '*' for xml path('th'), type),
                 (select @style as '@style', 'Jul' as '*' for xml path('th'), type),
                 (select @style as '@style', 'Aug' as '*' for xml path('th'), type),
                 (select @style as '@style', 'Sep' as '*' for xml path('th'), type),
                 (select @style as '@style', 'Oct' as '*' for xml path('th'), type),
                 (select @style as '@style', 'Nov' as '*' for xml path('th'), type),
                 (select @style as '@style', 'Dec' as '*' for xml path('th'), type),
                 (select @style as '@style', 'Total' as '*' for xml path('th'), type)


         for xml path('tr'), type
     ),
     (
         select 'trclass' as '@class',
                 (case when [PropertyCode]='A' then 'background-color:#d9dcf0' 
                       when [PropertyCode]='B' then 'background-color:#ebfadc' 
                       when [PropertyCode]='C' then 'background-color:#d9dcf0' 

                  end) as '@style',

                 (select StayYear as '*' for xml path('td'), type),
                 (select PropertyCode as '*' for xml path('td'), type),
                 (select Jan as '*' for xml path('td'), type),
                 (select Feb as '*' for xml path('td'), type),
                 (select Mar as '*' for xml path('td'), type),
                 (select Apr as '*' for xml path('td'), type),
                 (select May as '*' for xml path('td'), type),
                 (select Jun as '*' for xml path('td'), type),
                 (select Jul as '*' for xml path('td'), type),
                 (select Aug as '*' for xml path('td'), type),
                 (select Sep as '*' for xml path('td'), type),
                 (select Oct as '*' for xml path('td'), type),
                 (select Nov as '*' for xml path('td'), type),
                 (select Dec as '*' for xml path('td'), type),
                 (select 'font-weight:bold' as '@style', Total as '*' for xml path('td'), type)


         from @MyTable
         GROUP BY [StayYear], [PropertyCode], [Jan], [Feb], [Mar], [Apr], [May], [Jun], [Jul], [Aug], [Sep], [Oct], [Nov], [Dec], [Total]
         ORDER BY  [PropertyCode], [StayYear] DESC
         for xml path('tr'), type
     )

 for xml path('table')
 )

 select convert(xml,@tableHtml)

【讨论】:

太棒了!你拯救了我的一天!

以上是关于我的 SQL 查询中需要 HTML 代码来格式化带有千位分隔符的表格编号,并使总列值以粗体显示的主要内容,如果未能解决你的问题,请参考以下文章

我需要 sql 查询来获取所需的格式

sql server 数据库,在查询sql语句中日期格式转换问题,怎么把原数据年月日时分秒转换成年月日

SQL查询语句之查询数据

CONCAT sql 查询需要正确的格式。 Codeigniter 活动记录

SQL 查询帮助 - 错误的数据格式解决方法?

使用带参数的 LIKE 时 SQL 引发语法错误 [重复]