MS SQL to remove text from a string

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • pgoodson
    New Member
    • May 2013
    • 4

    MS SQL to remove text from a string

    ACCESS sql question: I have different length data in a field and I want to remove all the data in the string that is to the right of the '-' a dash. I can find the len of the string and I can find the position of the '-', but cannot seem to complete the syntax for the complete use

    ex: 32A23-001 or 24A-002 or 312-01
    I want to retreive the data before the dash only.
  • Seth Schrock
    Recognized Expert Specialist
    • Dec 2010
    • 2965

    #2
    If you are wanting the text that is to the right of the first '-', then you would use the Mid() function and set the starting point as the position of the '-' using the InStr() function. You might have to play with +/- 1 to get the exact starting location, but that is simple to play with.

    Comment

    • pgoodson
      New Member
      • May 2013
      • 4

      #3
      Seth, my example really is showing I want the chars to the left of the dash, not to the right and I am not getting the syntax with using mid/instr/len tying them all together.

      Comment

      • pgoodson
        New Member
        • May 2013
        • 4

        #4
        Here is what my ms access sql looks like right now and I am not getting the chars to the left of the dash:
        mid([LED data from RDR-rev2].LGEY_ISGT_STAL N_CD,InStr([LED data from RDR-rev2].[LGEY_ISGT_STALN _CD],"-"),len([LED data from RDR-rev2].LGEY_ISGT_STAL N_CD) - instr([LED data from RDR-rev2].[LGEY_ISGT_STALN _CD],"-")) as Left_DashPos

        Comment

        • pgoodson
          New Member
          • May 2013
          • 4

          #5
          Seth,
          With trial and error I finally got it using left/instr below, so thank you for reply so quickly!:
          left([LED data from RDR-rev2].LGEY_ISGT_STAL N_CD,InStr([LED data from RDR-rev2].[LGEY_ISGT_STALN _CD],"-")-1) as Left_DashPos

          Comment

          • Seth Schrock
            Recognized Expert Specialist
            • Dec 2010
            • 2965

            #6
            Sorry, after re-reading your OP, I'm not sure why I thought it said "remove text to the LEFT" when it clearly says right. My bad. Glad you found the solution anyway.

            Comment

            Working...