首页 > 解决方案 > SQL 查询输出 - 小数位,整数太多

问题描述

我有一个正常运行的查询,我正在合并两个表,删除重复项并将数据保留为最近的生效日期。

表格如下所示:

1.TAX_TABLE---------    
tax_id  description

        1   AZ State
        2   AZ-Maricopa Co
        4   AZ-Maricopa/Mesa



2.Tax_RATE_TABLE-------
tax_id  effective_date  tax_percent

1   2015-01-01 00:00:00.000 5.6
2   2015-01-01 00:00:00.000 0.7
4   2015-01-01 00:00:00.000 1.75
4   2019-03-01 00:00:00.000 2

我当前的查询如下所示:

use lhsitedb
select t.tax_id, t.description, tr.effective_date, tr.tax_percent
from dbo.tax t
inner join dbo.tax_rate tr on tr.tax_id = t.tax_id
where tr.effective_date = (
    select max(tr1.effective_date)
    from dbo.tax_rate tr1
    where tr1.tax_id = tr.tax_id

我的输出就是这样,我在 tax_tax 百分比列中得到小数位数。我一直在尝试对该列进行限制,以仅显示可以扩展到百分之一的十进制数字。

注意:此查询在 SSMS 中执行时工作得很好,但是,我正在使用sqlcmd该文件并将其导出到一个.txt文件中,该文件是产生这些意外结果的地方。

tax_id                description                     effective_date          tax_percent             
--------------------- ------------------------------- ----------------------- ------------------------
                    4 AZ-Maricopa/Mesa                2019-03-01 00:00:00.000                        2
                    2 AZ-Maricopa Co                  2015-01-01 00:00:00.000      0.69999999999999996
                    1 AZ State                        2015-01-01 00:00:00.000       5.5999999999999996

(17 rows affected)

当该列时,应该更多地沿着这些方向查看:

         tax_percent  
   ----------------------------
                   2 
                   1.75 

标签: sqlsql-serversql-server-2008

解决方案


更新了我的答案以使用 CONVERT 函数...

我会尝试使用该CONVERT功能..试一试!2 将指定百分之一。CONVERT(DECIMAL(10,2),tr.tax_percent) as 'tax_percent'

use lhsitedb
select t.tax_id, t.description, tr.effective_date, 
CONVERT(DECIMAL(10,2),tr.tax_percent) AS 
'tax_percent' 
from dbo.tax t
inner join dbo.tax_rate tr on tr.tax_id = t.tax_id
where tr.effective_date = (
    select max(tr1.effective_date)
    from dbo.tax_rate tr1
    where tr1.tax_id = tr.tax_id

推荐阅读