问题描述
我支持将平面文件输入转换为SqlServer数据库表的ETL过程。代码几乎是100%的T-SQL并在DB中运行。我不拥有代码,不能更改工作流。我只能帮助配置翻译SQL,它接收文件数据并将其转换为表数据(稍后将对此进行更多说明)。
现在免责声明方式...
我们的一个文件提供者最近更改了它们如何表示从'12345.67'
到 '12,345.67'
。转换值的SQL看起来像 SELECT FLOOR(CAST([inputValue] AS DECIMAL(24,10)))
,并且不再工作。
由于我必须将最终值存储为 Decimal(24,10)
datatype (是的,我意识到 FLOOR
清除所有小数点后的精度 - 设计器与客户不同步) ,
感谢你的想法。
请尝试使用:
SELECT REPLACE('12,345.67',',','')
OUTPUT:
12345.67
所以它是:
SELECT FLOOR(CAST(REPLACE([input value],',',')AS DECIMAL(24,10)))
pre>
I am supporting an ETL process that transforms flat-file inputs into a SqlServer database table. The code is almost 100% T-SQL and runs inside the DB. I do not own the code and cannot change the workflow. I can only help configure the "translation" SQL that takes the file data and converts it to table data (more on this later).
Now that the disclaimers are out of the way...
One of our file providers recently changed how they represent a monetary amount from
'12345.67'
to'12,345.67'
. Our SQL that transforms the value looks likeSELECT FLOOR( CAST([inputValue] AS DECIMAL(24,10)))
and no longer works. I.e., the comma breaks the cast.Given that I have to store the final value as
Decimal (24,10)
datatype (yes, I realize theFLOOR
wipes out all post-decimal-point precision - the designer was not in sync with the customer), what can I do to cast this string efficiently?'Thank you for your ideas.
解决方案try using REPLACE (Transact-SQL):
SELECT REPLACE('12,345.67',',','')
OUTPUT:
12345.67
so it would be:
SELECT FLOOR( CAST(REPLACE([input value],',','') AS DECIMAL(24,10)))
这篇关于将带逗号分隔符的nVarChar转换/转换为十进制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!