Get value after certain characters

  • Hello!

    I have a column with this data:

    A~B~C

    How would I just get the last value "C"

    select left('A~B~C.ab', LEN('A~B~C.ab') - CHARINDEX('\', REVERSE('A~B~C.ab')) + 1),RIGHT('A~B~C.ab', CHARINDEX('\', REVERSE('A~B~C.ab')) - 1)

    I am using this and getting Invalid length parameter passed to the right function.

     

    Thanks.

     

     

     

  • ;WITH test_data AS (
    SELECT 'A~B~C.ab' AS data
    UNION ALL
    SELECT 'D~E~FG.hij'
    )
    SELECT data, data_result
    FROM test_data
    CROSS APPLY (
    SELECT LEFT(data, LEN(data) - CHARINDEX('.', REVERSE(data))) AS data1
    ) AS ca1
    CROSS APPLY (
    SELECT RIGHT(data1, CHARINDEX('~', REVERSE(data1)) - 1) AS data_result
    ) AS ca2

    SQL DBA,SQL Server MVP(07, 08, 09) "It's a dog-eat-dog world, and I'm wearing Milk-Bone underwear." "Norm", on "Cheers". Also from "Cheers", from "Carla": "You need to know 3 things about Tortelli men: Tortelli men draw women like flies; Tortelli men treat women like flies; Tortelli men's brains are in their flies".

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

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