如何让 SSIS 数据流将“0.00”放入在平面文件中?

发布于 2024-09-02 01:06:23 字数 870 浏览 13 评论 0原文

我有一个带有数据流的 SSIS 包,它采用 ADO.NET 数据源(只是一个小表),执行 select * 查询,并将查询结果输出到平面文件(我也尝试过只提取整个表)并且不使用 SQL select)。

问题是数据源拉取 Money 数据类型的列,如果值不为零,它会很好地进入文本平面文件(如“123.45”),但是当值为零时,它会显示在目标平面文件中为“.00”。我需要知道如何将前导零恢复到平面文件中。

我已经尝试了各种输出数据类型(在平面文件连接管理器中),包括货币和字符串,但这似乎没有效果。

我在我的选择中尝试了一个 case 语句,如下所示:(

  CASE WHEN columnValue = 0 THEN 
     '0.00' 
  ELSE 
      columnValue 
  END

仍然结果为“.00”)

我尝试过类似的变体:(

 CASE WHEN columnValue = 0 THEN
     convert(decimal(12,2), '0.00') 
 ELSE 
     convert(decimal(12,2), columnValue) 
 END

仍然结果为“.00”)

和:(

 CASE WHEN columnValue = 0 THEN
     convert(money, '0.00') 
 ELSE 
     convert(money, columnValue) 
 END

结果为“.0000000000000000000” ')

这个愚蠢的小问题快要了我的命。谁能告诉我如何将零 Money 数据类型数据库值作为“0.00”放入平面文件中?

I have an SSIS package with a Data Flow that takes an ADO.NET data source (just a small table), executes a select * query, and outputs the query results to a flat file (I've also tried just pulling the whole table and not using a SQL select).

The problem is that the data source pulls a column that is a Money datatype, and if the value is not zero, it comes into the text flat file just fine (like '123.45'), but when the value is zero, it shows up in the destination flat file as '.00'. I need to know how to get the leading zero back into the flat file.

I've tried various datatypes for the output (in the Flat File Connection Manager), including currency and string, but this seems to have no effect.

I've tried a case statement in my select, like this:

  CASE WHEN columnValue = 0 THEN 
     '0.00' 
  ELSE 
      columnValue 
  END

(still results in '.00')

I've tried variations on that like this:

 CASE WHEN columnValue = 0 THEN
     convert(decimal(12,2), '0.00') 
 ELSE 
     convert(decimal(12,2), columnValue) 
 END

(Still results in '.00')

and:

 CASE WHEN columnValue = 0 THEN
     convert(money, '0.00') 
 ELSE 
     convert(money, columnValue) 
 END

(results in '.0000000000000000000')

This silly little issue is killin' me. Can anybody tell me how to get a zero Money datatype database value into a flat file as '0.00'?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

梦行七里 2024-09-09 01:06:23

我遇到了完全相同的问题,soo 的回答对我有用。我将数据发送到派生列转换(在数据流转换工具箱中)。我将派生列添加为数据类型 Unicode String ([DT_WSTR]) 的新列,并使用以下表达式:

Price < 1 ? “0”+(DT_WSTR,6)价格:(DT_WSTR,6)价格

我希望有帮助!

I was having the exact same issue, and soo's answer worked for me. I sent my data into a derived column transform (in the Data Flow Transform toolbox). I added the derived column as a new column of data type Unicode String ([DT_WSTR]), and used the following expression:

Price < 1 ? "0" + (DT_WSTR,6)Price : (DT_WSTR,6)Price

I hope that helps!

疾风者 2024-09-09 01:06:23

您可以使用派生列来更改值的格式吗?你尝试过吗?

Could you use a Derived Column to change the format of the value? Did you try that?

萌酱 2024-09-09 01:06:23

我使用高级编辑器将列从双精度浮点数更改为小数,然后将比例设置为 2:

在此处输入图像描述

I used the advanced editor to change the column from double-precision float to decimal and then set the Scale to 2:

enter image description here

梦中的蝴蝶 2024-09-09 01:06:23

由于您要导出到文本文件,因此只需导出预先格式化的数据。

您可以在查询中执行此操作或创建派生列,无论您更喜欢什么。

我选择将该列设为 15 个字符宽。如果您导入一个需要数字的系统,那么这些零应该被忽略......那么为什么不直接标准化字段长度呢?

SQL 中的一个简单解决方案如下:

    select 
    cast(0.00 as money) as col1
    ,cast(0.00 as numeric(18,2)) as col2 
    ,right('000000000000000' + cast( 0.00 as varchar(10)), 15) as col3
    go

 col1                  col2                 col3
 --------------------- -------------------- ---------------
                 .0000                  .00 000000000000.00

只需将“0.00”替换为您的列名,并且不要忘记添加 FROM table_name 等。

Since you are exporting to text file, just export data preformatted.

You can do it in the query or create a derived column, whatever you are more comfortable with.

I chose to make the column 15 characters wide. If you import into a system that expects numbers those zeros should be ignored...so why not just standardize the field length?

A simple solution in SQL is as follows:

    select 
    cast(0.00 as money) as col1
    ,cast(0.00 as numeric(18,2)) as col2 
    ,right('000000000000000' + cast( 0.00 as varchar(10)), 15) as col3
    go

 col1                  col2                 col3
 --------------------- -------------------- ---------------
                 .0000                  .00 000000000000.00

Simply replace '0.00' with your column name and don't forget to add the FROM table_name, etc..

流年已逝 2024-09-09 01:06:23

使用派生列很好,也需要检查条件
价格检查<=0? "0" + (DT_WSTR,10)pricecheck : (DT_WSTR,10)pricecheck

或者替代方法是使用 vb 脚本

在此处输入图像描述

在此处输入图像描述

It is good to use derived column and need to check the condition as well
pricecheck <=0 ? "0" + (DT_WSTR,10)pricecheck : (DT_WSTR,10)pricecheck

or alternative way is to use vb script

enter image description here

enter image description here

煮酒 2024-09-09 01:06:23

最终我最终做的是使用 FORMAT() 函数。

CAST(FORMAT(balance, '0000000000.0000') AS varchar(30)) AS "balance"

由于 SQL Server 实现该功能的方式,这确实对 CPU 性能产生了一些显着影响(通常至少一个数量级),但对我来说,没有什么比这更容易、更正确或更一致地工作了。我处理的行数少于 100,000 行,并且该包每小时执行不超过一次。在我的情况下,从 100 毫秒到 1000 毫秒并不是什么大问题。

默认情况下,FORMAT() 函数返回一个 nvarchar(4000),因此我还将其转换回适当大小的 varchar,因为我的输出文件需要位于 Windows 中- 1252 编码。在 SSIS 中,对文本进行转码比它应有的要令人讨厌得多。

Ultimately what I ended up doing was using the FORMAT() function.

CAST(FORMAT(balance, '0000000000.0000') AS varchar(30)) AS "balance"

This does have some significant CPU performance impact (often at least an order of magnitude) due to the way SQL Server implements that function, but nothing worked easier, more correctly, or more consistently for me. I was working with less than 100,000 rows and the package executes no more than once an hour. Going from 100ms to 1000ms just wasn't a big deal in my situation.

The FORMAT() function returns an nvarchar(4000) by default, so I also cast it back to a varchar of appropriate size since my output file needed to be in Windows-1252 encoding. Transcoding text is much more obnoxious in SSIS than it has any right to be.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文