Results 1 to 6 of 6
  1. #1
    Kirtap is offline Novice
    Windows 7 64bit Access 2010 32bit
    Join Date
    Oct 2013
    Posts
    22

    Replace text before and including - to be empty

    Hello,
    I'm trying to get numbers in some text after a -.

    Here is how the text is formated:


    D##-###.
    So like an example would be: D14-013

    I thought this query would get that last bit:

    SELECT Replace(GPCL_Asset_ID,'D%-','') AS Expr1
    FROM Assets
    WHERE Assets.GPCL_Asset_ID Like 'D%' AND Assets.GPCL_Asset_ID NOT Like '%IH%';

    However it returns the text as it was (i.e. D14-013 instead of 013).

    I'm not sure how to approach this. I thought that this would be the best approach.
    My ultimate goal is to find the largest number after the -.

    So it end up like:


    SELECT Max(CInt(Replace(GPCL_Asset_ID,'D%-',''))) AS Expr1
    FROM Assets
    WHERE Assets.GPCL_Asset_ID Like 'D%' AND Assets.GPCL_Asset_ID NOT Like '%IH%';

    Any help would be appreciated.

    Thank you in advance.

    Edit: I could do Right(GPCL_Asset_ID, 3). But I'm trying to also account for the possibility it becomes four digits long.

  2. #2
    JoeM is offline VIP
    Windows XP Access 2007
    Join Date
    Jun 2012
    Posts
    3,904
    I'm trying to get numbers in some text after a -.
    Try this:
    Code:
    Expr1: Mid([GPCL_Asset_ID],InStr([GPCL_Asset_ID],"-")+1,Len([GPCL_Asset_ID]))

  3. #3
    Kirtap is offline Novice
    Windows 7 64bit Access 2010 32bit
    Join Date
    Oct 2013
    Posts
    22
    Quote Originally Posted by JoeM View Post
    Try this:
    Code:
    Expr1: Mid([GPCL_Asset_ID],InStr([GPCL_Asset_ID],"-")+1,Len([GPCL_Asset_ID]))
    This works perfectly! Thank you so much!
    I'll have to take a look at the Mid function to get a better understanding. Thanks for sending me in the right direction!

  4. #4
    JoeM is offline VIP
    Windows XP Access 2007
    Join Date
    Jun 2012
    Posts
    3,904
    The LEFT, RIGHT, and MID function are just Text functions that tell you where to start in taking characters from a text string. MID tells it to start somewhere in the middle.
    The INSTR function tells it to locate where the "-" can be found in your string. Once you found that, you want to start taking the characters just after that (hence the "+1").
    So that is how we find the starting point of our MID function.

    The last argument of the MID function tells you how many characters to take. Using the LEN function to get the Length of our string. It actually tells it to take more than we need (since we are starting in the middle and not at the beginning), but that doesn't hurt anything.


    See:
    http://www.techonthenet.com/access/f...string/mid.php
    and
    http://www.techonthenet.com/access/f...ring/instr.php

  5. #5
    Kirtap is offline Novice
    Windows 7 64bit Access 2010 32bit
    Join Date
    Oct 2013
    Posts
    22
    Thank you very much for the explanation! I really appreciate it. Now I have a better understanding of these functions.

  6. #6
    orange's Avatar
    orange is offline Moderator
    Windows XP Access 2003
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,726

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 2
    Last Post: 07-21-2012, 01:21 AM
  2. End of Text Box - Replace Comma with Period
    By bburton in forum Programming
    Replies: 14
    Last Post: 06-20-2011, 03:20 PM
  3. Replace a string/text in ms-access db
    By anziga in forum Queries
    Replies: 4
    Last Post: 12-31-2010, 06:40 PM
  4. Replies: 20
    Last Post: 09-18-2010, 02:31 PM
  5. replace a empty field with the word "none" how??
    By techexpressinc in forum Queries
    Replies: 1
    Last Post: 01-15-2010, 11:02 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums