Need SQL to strip off anything to right of slash

  • Looking for SQL to render a string that truncates anything to the right of the character "\"

    3 examples:

    1) ServerName1\Dev should render as ServerName1

    2) SN1\Development should render as SN1

    3) ABCD\1234 should render as ABCD

    any suggestions are greatly appreciated...

    BT
  • declare @str1 varchar(100)

    set @str1='ServerName1\Dev'

    select left(@str1,charindex('\',@str1)-1)

    I've assumed there is only one backslash in the string!!



    Pradeep Singh

  • Try this...

    CREATE TABLE #tempa

    (

    idcol INT IDENTITY(1,1),

    NAME VARCHAR(100))

    INSERT INTO #tempa (NAME)

    SELECT 'ServerName1\Dev'

    INSERT INTO #tempa (NAME)

    SELECT 'SN1\Development'

    INSERT INTO #tempa (NAME)

    SELECT 'ABCD\1234'

    SELECT SUBSTRING(NAME,1,CHARINDEX('\',NAME,1)-1) FROM #tempa

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

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