How to remove string

  • Hi,

    TableA

    Col1

    ----

    13.4

    13.4 a

    13..4

    13,.4

    Result for Table A needed:

    Col1 ColCalculated

    --------------------------

    13.4 13.4

    13.4 a null

    13..4 null

    13,.4 null

    Please let me know how can I achieve it.

    Thanks in advance.

  • In SQL Server 2012, you can use TRY_PARSE(), TRY_CONVERT() or TRY_CAST().

    Luis C.
    General Disclaimer:
    Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?

    How to post data/code on a forum to get the best help: Option 1 / Option 2
  • Simple and maybe not perfect approach is like Luis mentioned, using try_parse. The drawback is that some unwanted values may parse but then again, that might be fine.

    😎

    ;WITH TableA(Col1)

    AS (SELECT * FROM (VALUES

    ('13.4' )

    ,('13.4 a' )

    ,('13..4' )

    ,('13,.4' )) AS X(Col1))

    SELECT

    TA.Col1

    ,TRY_PARSE(TA.Col1 AS DECIMAL(12,2)) AS CalculatedColumn

    FROM TableA TA

    Results

    Col1 CalculatedColumn

    ------ -----------------

    13.4 13.40

    13.4 a NULL

    13..4 NULL

    13,.4 13.40

  • Great 🙂

    Thanks

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

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