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 7 (of 7 total)

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