Results 1 to 5 of 5
  1. #1
    Join Date
    Jun 2009
    Posts
    11

    Unanswered: Substring the middle Value in SQl 2005

    Hi

    I want get the value in beween '-' using sql query 2005
    Eg: IT-112-STR I want to get 112
    IT-1567-WDR I want to get 1567


    Thanks & Regards
    Nishajk
    Last edited by nishajk; 09-19-11 at 05:56.

  2. #2
    Join Date
    Feb 2004
    Location
    Bangalore, India
    Posts
    250
    will the column have more than 2 '-'s ?
    Cheers....

    baburajv

  3. #3
    Join Date
    Jun 2009
    Posts
    11

    Substring the middle Value

    Quote Originally Posted by baburajv View Post
    will the column have more than 2 '-'s ?
    Yes..column contains Mutiple '-'s

  4. #4
    Join Date
    Nov 2004
    Posts
    1,427
    Provided Answers: 4
    When you only need the text between the first two '-'s, this will give the required result:
    Code:
    DECLARE @AString CHAR(50)
    SET @AString = 'IT-112-STR'
    SELECT SUBSTRING(@AString, CHARINDEX ('-', @AString)+1, CHARINDEX ('-', @AString, CHARINDEX ('-', @AString) + 1) - CHARINDEX ('-', @AString) - 1)  
    
    SET @AString = 'IT-1567-WDR'
    SELECT SUBSTRING(@AString, CHARINDEX ('-', @AString)+1, CHARINDEX ('-', @AString, CHARINDEX ('-', @AString) + 1) - CHARINDEX ('-', @AString) - 1)
    With kind regards . . . . . SQL Server 2000/2005/2012
    Wim

    Grabel's Law: 2 is not equal to 3 -- not even for very large values of 2.
    Pat Phelan's Law: 2 very definitely CAN equal 3 -- in at least two programming languages

  5. #5
    Join Date
    Jun 2009
    Posts
    11

    Thank you

    Hi

    I got the Answer... Thank you ery much..

    Regards
    NishaJK

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •