right of last instance of characters

  • memememe::kjsdfkjfds::ksjf::iuhsaf

    sdffsd::safsdf

    sdffs::sdfsdf:11231231::safdsfds::sdffssdf::sdfs

    sdfsdffsdfsdf::sdfsdf::sfsdf::sdfsdf::sadfdssfds

    dsffsfdfsdfsfdsfddf::sdfsfsfdfdssd::sdfsddfsdf

     

    field in table contains the example data above I need a select query where it selects everything right of the last occurrance of :: very easy to do with left and first instance of :: but struggling how to do it for last instance and right .... any help

  • Use the left and first instance method, on the REVERSE of the string.

  • declare @STR varchar(8000)
    set @STR = 'a:bb:cc:'
    select
    substring(@str,len(@str)-charindex(':',reverse(@str))+2,8000)

     

    that way you don't need to reverse three times to do it inline.

    Tim Wilkinson

    "If it doesn't work in practice, you're using the wrong theory"
    - Immanuel Kant

  • Not in TSQL you can't.

    [edit: oh i see it's a UDF. There are better ways of splitting strings though. But this request was not about doing that anyway.]

    Tim Wilkinson

    "If it doesn't work in practice, you're using the wrong theory"
    - Immanuel Kant

  • You can use

    select * from Split('memememe::kjsdfkjfds::ksjf::iuhsaf

    sdffsd::safsdf

    sdffs::sdfsdf:11231231::safdsfds::sdffssdf::sdfs

    sdfsdffsdfsdf::sdfsdf::sfsdf::sdfsdf::sadfdssfds

    dsffsfdfsdfsfdsfddf::sdfsfsfdfdssd::sdfsddfsdf','::')

    http://www.sqlservercentral.com/memberservices/updatescript.asp?Approve=y&scriptid=850

  • I didn't use a double colon, but it works for any string (subject to length restrictions).

    declare

    @STR varchar(8000)

    set

    @STR = 'ad::d'

    select

    substring

    (@STR,len(@STR)-charindex('::',reverse(@STR))+2,8000)

    Tim Wilkinson

    "If it doesn't work in practice, you're using the wrong theory"
    - Immanuel Kant

  • sorry I did not say thanks earlier but been away, so thanks,

Viewing 7 posts - 1 through 6 (of 6 total)

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