Remove last character in query

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • josh64057
    New Member
    • Dec 2011
    • 3

    #1

    Remove last character in query

    I am trying to remove the last character using a query in access 2010. I only want to remove the last character if the lenght is 7 characters. If i use Left([CASCADE_ID],Len([CASCADE_ID])-1). I end up removing both "T" from the field that has 6 and 7 characters. I just want it to remove the "T" from the field with 7 charactes.

    CT107T = do not change
    CT106TT = need to change to CT106T
  • ADezii
    Recognized Expert Expert
    • Apr 2006
    • 8834

    #2
    Code:
    UPDATE tblTest SET tblTest.CASCADE_ID = IIf(Len([CASCADE_ID])=7,Left$([CASCADE_ID],Len([CASCADE_ID])-1),[CASCADE_ID]);

    Comment

    • josh64057
      New Member
      • Dec 2011
      • 3

      #3
      This work great. thank you!

      Comment

      • Rabbit
        Recognized Expert MVP
        • Jan 2007
        • 12517

        #4
        Are they only at most 7 characters long? You can simplify by using left(field, 6)

        Comment

        • josh64057
          New Member
          • Dec 2011
          • 3

          #5
          some of them are 10, but i need them to be 9. just like i needed the 7 to be 6.

          Comment

          • ADezii
            Recognized Expert Expert
            • Apr 2006
            • 8834

            #6
            You never mentioned about the additional requirement for a String whose Length is 10. A slight change in the SQL Statement is required:
            Code:
            UPDATE tblTest SET tblTest.CASCADE_ID = IIf(Len([CASCADE_ID])=7, _
            Left$([CASCADE_ID],Len([CASCADE_ID])-1),IIf(Len([CASCADE_ID])=10, _
            Left$([CASCADE_ID],Len([CASCADE_ID])-1),[CASCADE_ID]));

            Comment

            • Mihail
              Contributor
              • Apr 2011
              • 759

              #7
              I think that this will work as well but is a little bit shortly than Rabit's (from where I had inspired):

              Code:
              IIF((Len(Cascade_ID)=7) OR (Len(Cascade_ID)=10),Left$([CASCADE_ID],Len([CASCADE_ID])-1),[CASCADE_ID])
              More than, you can include as many OR clauses as you need without using a new IIF function for that.

              Comment

              • ADezii
                Recognized Expert Expert
                • Apr 2006
                • 8834

                #8
                @Mihail:
                Cleaner approach, and better than using Nested IIfs().

                Comment

                • NeoPa
                  Recognized Expert Moderator MVP
                  • Oct 2006
                  • 32669

                  #9
                  Originally posted by Josh
                  Josh:
                  some of them are 10, but i need them to be 9. just like i needed the 7 to be 6.
                  Why not specify the question clearly instead of just providing examples of exceptions only after the previous version of the question's solution(s) have been posted? That may save people wasting their efforts in this way.

                  Comment

                  • Mihail
                    Contributor
                    • Apr 2011
                    • 759

                    #10
                    Sorry ADezii. I don't know why I write Rabit. Maybe because Rabit (like you) help me a lot.

                    Comment

                    Working...