将带逗号分隔符的nVarChar转换/转换为十进制 [英] Converting / Casting an nVarChar with Comma Separator to Decimal

查看:143
本文介绍了将带逗号分隔符的nVarChar转换/转换为十进制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我支持将平面文件输入转换为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 清除所有小数点后的精度 - 设计器与客户不同步)



感谢你的想法。

解决方案

请尝试使用 REPLACE(Transact- SQL)

  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 like SELECT 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 the FLOOR 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转换/转换为十进制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆