locked
How can I convert a string value to decimal ? RRS feed

  • Question

  • User1253338400 posted

    Hi,

    I am trying to do the following on a string but the string has been imported incorrectly so i need to accommodate for the scenario.. The query is the following: 

    SELECT cast('3,035.32' as decimal(28,10)) VALUE

    but get the following error 

    Error converting data type varchar to numeric.

    It is caused by the  ',' . Is there a way i can ignore the ',' and just get 

    SELECT cast('3035.32' as decimal(28,10)) VALUE

    Thanks

    Monday, October 17, 2016 8:36 AM

Answers

  • User-1716253493 posted
    SELECT cast(REPLACE('3,035.32',',','') as decimal(28,10)) VALUE

    Replace ',' with '' (empty)

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, October 17, 2016 8:41 AM

All replies

  • User-1716253493 posted
    SELECT cast(REPLACE('3,035.32',',','') as decimal(28,10)) VALUE

    Replace ',' with '' (empty)

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, October 17, 2016 8:41 AM
  • User1253338400 posted

    Thanks so much , works perfect.

    Monday, October 17, 2016 8:43 AM
  • User77042963 posted
    SELECT TRY_PARSE('3,035.32'  as decimal(28,10))  ---SQL Server 2012  or up

    Monday, October 17, 2016 1:54 PM