Convert data to numeric/decimal

  • I have a varchar column that has amounts that are mixed ie
    42,180.00
    $123.44
    4,555.99
    £100,000
    $1,393.21

    I want to create a new column with those amounts above as a decimal/numeric type column.  How can i do this?

  • GrassHopper - Thursday, October 18, 2018 10:09 AM

    I have a varchar column that has amounts that are mixed ie
    42,180.00
    $123.44
    4,555.99
    £100,000
    $1,393.21

    I want to create a new column with those amounts above as a decimal/numeric type column.  How can i do this?

    select CONVERT(decimal(10,2),REPLACE(REPLACE(REPLACE(T.C,'$',''),'£',''),',','')) myDecimalColumn, *
    from (values ('42,180.00'),
    ('$123.44'),
    ('4,555.99'),
    ('£100,000'),
    ('$1,393.21')) T(C)

  • Jonathan AC Roberts - Thursday, October 18, 2018 10:17 AM

    GrassHopper - Thursday, October 18, 2018 10:09 AM

    I have a varchar column that has amounts that are mixed ie
    42,180.00
    $123.44
    4,555.99
    £100,000
    $1,393.21

    I want to create a new column with those amounts above as a decimal/numeric type column.  How can i do this?

    select CONVERT(decimal(10,2),REPLACE(REPLACE(REPLACE(T.C,'$',''),'£',''),',','')) myDecimalColumn, *
    from (values ('42,180.00'),
    ('$123.44'),
    ('4,555.99'),
    ('£100,000'),
    ('$1,393.21')) T(C)

    Haven't tried this particular set of data but it might be a little faster to simply convert them to the MONEY datatype and then to decimal.  The MONEY datatype isn't bothered by commas and valid currency markers.

    Of bigger concern might be the fact that once these are converted to decimal, the currency indications will be gone unless other measures are taken.

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

  • Jonathan AC Roberts - Thursday, October 18, 2018 10:17 AM

    GrassHopper - Thursday, October 18, 2018 10:09 AM

    I have a varchar column that has amounts that are mixed ie
    42,180.00
    $123.44
    4,555.99
    £100,000
    $1,393.21

    I want to create a new column with those amounts above as a decimal/numeric type column.  How can i do this?

    select CONVERT(decimal(10,2),REPLACE(REPLACE(REPLACE(T.C,'$',''),'£',''),',','')) myDecimalColumn, *
    from (values ('42,180.00'),
    ('$123.44'),
    ('4,555.99'),
    ('£100,000'),
    ('$1,393.21')) T(C)

    This works great when all rows are populated with a value.  There are rows that have blank values  or just a "-" (negative sign).  How do i get around that?

  • GrassHopper - Thursday, October 18, 2018 11:06 AM

    Jonathan AC Roberts - Thursday, October 18, 2018 10:17 AM

    GrassHopper - Thursday, October 18, 2018 10:09 AM

    I have a varchar column that has amounts that are mixed ie
    42,180.00
    $123.44
    4,555.99
    £100,000
    $1,393.21

    I want to create a new column with those amounts above as a decimal/numeric type column.  How can i do this?

    select CONVERT(decimal(10,2),REPLACE(REPLACE(REPLACE(T.C,'$',''),'£',''),',','')) myDecimalColumn, *
    from (values ('42,180.00'),
    ('$123.44'),
    ('4,555.99'),
    ('£100,000'),
    ('$1,393.21')) T(C)

    This works great when all rows are populated with a value.  There are rows that have blank values  or just a "-" (negative sign).  How do i get around that?

    Use Jeff's solution:
    select convert(decimal(9,2),convert(money,T.C)) myDecimalColumn,*
    from (values ('42,180.00'),
    ('$123.44'),
    ('4,555.99'),
    ('£100,000'),
    ('-'),
    (''),
    ('$1,393.21')) T(C)

Viewing 5 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic. Login to reply