Separate field based on \ character

  • I have the below field...

    servername\instance

    I want to be listed as two separate fields in my report

    servername instance

    How do I make these two separate fields? Substring?

  • Can use this to make seperate fileds...

    select substring(name,1, len(name)-(charindex('\',reverse(name))))

    ,substring(name,len(name)-(charindex('\',reverse(name)))+2,len(name))

    from instancename

    Can use this to be same field replacing \ with ' '...

    select Replace(name,'\',' ')

    from instancename

    John

  • a neat trick with the PARSENAME function, which is used to chop up object names like ServerName.DatabaseName.SchemaName.ObjectName:

    --Results:

    /*

    ServerName Instance

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

    MyServer SQL2005

    */

    SELECT

    PARSENAME(Replace(instancename,'\','.'),2) AS ServerName,

    PARSENAME(Replace(instancename,'\','.'),1) AS Instance

    from(SELECT 'MyServer\SQL2005' AS instancename) x

    Lowell


    --help us help you! If you post a question, make sure you include a CREATE TABLE... statement and INSERT INTO... statement into that table to give the volunteers here representative data. with your description of the problem, we can provide a tested, verifiable solution to your question! asking the question the right way gets you a tested answer the fastest way possible!

  • The following will also work:

    declare @TestString varchar(32) = 'servername\instance';

    select

    left(@TestString,charindex('\', @TestString) - 1),

    right(@TestString, len(@TestString) - charindex('\', @TestString));

  • Great they both worked...thank you everyone!

  • Lowell (6/13/2012)


    a neat trick with the PARSENAME function, which is used to chop up object names like ServerName.DatabaseName.SchemaName.ObjectName:

    --Results:

    /*

    ServerName Instance

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

    MyServer SQL2005

    */

    SELECT

    PARSENAME(Replace(instancename,'\','.'),2) AS ServerName,

    PARSENAME(Replace(instancename,'\','.'),1) AS Instance

    from(SELECT 'MyServer\SQL2005' AS instancename) x

    Lowell - That is really slick!


    My mantra: No loops! No CURSORs! No RBAR! Hoo-uh![/I]

    My thought question: Have you ever been told that your query runs too fast?

    My advice:
    INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
    The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.

    Need to UNPIVOT? Why not CROSS APPLY VALUES instead?[/url]
    Since random numbers are too important to be left to chance, let's generate some![/url]
    Learn to understand recursive CTEs by example.[/url]
    [url url=http://www.sqlservercentral.com/articles/St

  • doesnt work for a list of named and default instances tho

  • DECLARE @SomeString VARCHAR(20)

    SET @SomeString = 'ServerName\Instance'

    If (SELECT LEN(@SomeString) - LEN(REPLACE( @SomeString,'\','')))>0

    SELECT LEFT(@SomeString,CHARINDEX('\',@SomeString)-1), right(@SomeString, len(@SomeString) - charindex('\', @SomeString));

    ELSE

    SELECT @SomeString

  • Thanks

Viewing 9 posts - 1 through 8 (of 8 total)

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