Converting / Casting an nVarChar with Comma Separator to Decimal

后端 未结 4 775
无人共我
无人共我 2020-12-11 17:11

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

4条回答
  •  囚心锁ツ
    2020-12-11 17:46

    While not necessarily the best approach for my situation, I wanted to leave a potential solution for future use that we uncovered while researching this problem.

    It appears that the SqlServer datatype MONEY can be used as a direct cast for strings with a comma separating the non-decimal portion. So, where SELECT CAST('12,345.56' AS DECIMAL(24,10)) fails, SELECT CAST('12,345.56' AS MONEY) will succeed.

    One caveat is that the MONEY datatype has a precision of 4 decimal places and would require an explicit cast to get it to DECIMAL, should you need it.

提交回复
热议问题