提问人:MaggieW 提问时间:9/30/2023 最后编辑:MaggieW 更新时间:9/30/2023 访问量:45
尝试转换和 CAST 不会将“10.00000”更改为“10”
Trying Convert and CAST is not changing "10.00000" to "10"
问:
我有一个表,其中所有列都设置为 varchar。这是必需的,因为文件中的数据在列中包含多种数据类型。
(PS 我尝试将数据加载为表格,但没有成功)
前 6 行是从文件加载的。文件的格式是必需的。我使用第 0 列中的值来生成传递给数据流任务的动态 SQL 语句。下面的查询是从表的第 1-6 行生成的动态查询。这不是问题查询。
SELECT REQ_NUMBER
,LINE_NBR
,LTrim(Rtrim(ITEM)) AS ITEM
,[DESCRIPTION]
,LTrim(Rtrim(MANUF_NBR)) AS MANUF_NBR
,ENTERED_UOM
,Convert(varchar(5),Cast(QUANTITY AS Integer)) as newqty
,Convert(varchar(5),Cast(KILL_QUANTITY AS Integer)) as newkillqty
,Convert(varchar(5),Cast((QUANTITY - KILL_QUANTITY) AS Integer)) as newpickqty
FROM [LSLMDB].[ls_apps].[REQLINE]
WHERE REQ_NUMBER LIKE 2286260
AND LINE_NBR <> 3
AND LINE_NBR <> 2
AND LINE_NBR <> 1
AND ITEM_TYPE like 'I'
and FILL_OR_KILL like 'K'
当我对源数据库执行上述 sql 时,它会返回预期的 10。但数据流将其追加到上表中,该表显示 10.00000。当我使用 SSMS 从目标数据库的表中选择行时,它显示 10.00000。到目前为止,当我将表提取到平面文件时,我尝试的任何内容都不会更改为 10.00000 到 10。我已经尝试了堆栈溢出帖子中建议的方法。 问题查询是:
SELECT
[Column 0]
,[Column 1]
,[Column 2]
,[Column 3]
,[Column 4]
,[Column 5] --making this result in 10,not 10.00000 in the flat file
,[Column 6] --making this result in 10,not 10.00000 in the flat file
,[Column 6] --making this result in 0,not 0.00000 in the flat file
,[Column 7]
,[Column 8]
,[Column 9]
,[Column 10]
,[Column 11]
,[Column 12]
,[Column 13]
,[Column 14]
,[Column 15]
,[Column 16]
,[Column 17]
FROM [Voxware].[dbo].[VoxPackListIn]
--CONVERT(varchar(5),CAST([Column 6] AS Integer)) as [Column 6],
--CONVERT(VARCHAR(5),TRUNC(CAST([Column 5] AS NUMERIC)))
--CONVERT(varchar(5),CAST(ABS(QUANTITY) AS Integer)) AS QUANTITY
--CONVERT(VARCHAR(5),(CAST(ROUND([Column 5],0) AS NUMERIC)))
--Cast(QUANTITY AS Integer)
我敢肯定还有其他迭代,但我已经这样做了 2 天,并没有保存不起作用的内容。该文件始终包含 10.00000。
答:
0赞
MaggieW
9/30/2023
#1
Josh 提供的解决方案有效。我使用案例陈述来确定是否应该进行转换。
SELECT
[Column 0]
,[Column 1]
,[Column 2]
,[Column 3]
,[Column 4]
,CASE
when seqnum > 3 then CONVERT(VARCHAR(5),CONVERT(INTEGER,CONVERT(FLOAT,[Column 5]))) --making this result in 10,not 10.00000 in the flat file
Else [column 5]
End as newcol5
,CASE
when seqnum > 3 then CONVERT(VARCHAR(5),CONVERT(INTEGER,CONVERT(FLOAT,[Column 6]))) --making this result in 10,not 10.00000 in the flat file
Else [column 6]
End as newcol6
,CASE
when seqnum > 3 then CONVERT(VARCHAR(5),CONVERT(INTEGER,CONVERT(FLOAT,[Column 7]))) --making this result in 10,not 10.00000 in the flat file
Else [column 7]
End as newcol7
,[Column 7]
,[Column 8]
,[Column 9]
,[Column 10]
,[Column 11]
,[Column 12]
,[Column 13]
,[Column 14]
,[Column 15]
,[Column 16]
,[Column 17]
FROM [Voxware].[dbo].[VoxPackListIn]
评论
SELECT REQ_NUMBER, LINE_NBR, ITEM, [DESCRIPTION], MANUF_NBR, ENTERED_UOM, ROUND(TRY_CAST(QUANTITY AS DECIMAL(18, 0)), 0) AS QUANTITY_INT, ROUND(TRY_CAST(KILL_QUANTITY AS DECIMAL(18, 0)), 0) AS KILL_QUANTITY_INT, ROUND(TRY_CAST(QUANTITY AS DECIMAL(18, 0)), 0) - ROUND(TRY_CAST(KILL_QUANTITY AS DECIMAL(18, 0)), 0) AS PICK_QUANTITY_INT FROM SampleTable;