Results 1 to 5 of 5
  1. #1
    learning_graccess is offline Advanced Beginner
    Windows XP Access 2007
    Join Date
    Oct 2011
    Posts
    40

    how to trim text in a column using sql query of ms access

    I have a column in a table (just for e.g.) like "3kg pen blue", "2kg pen white", "3 pencil red"

    Now I want to trim this into only the item name like "pen", "pencil". I don't want 3kg or blue. How it can be done in sql query in ms access?

  2. #2
    hertfordkc is offline 18 year novice
    Windows XP Access 2007
    Join Date
    Mar 2011
    Posts
    481

    I don't see your problem as one involving SQL as much as the VBA

    string functions. If you are wanting the extract the middle group of characters from a string containing 3 groups of characters separated by blanks, you probably need to use INSTR and MID and LEN. Google 'Access string functions'.
    I find working with query design much easier than trying to write SQL, so I would create a calculated field to extract the result, then go to SQL view to copy the SQL to where its needed.

  3. #3
    learning_graccess is offline Advanced Beginner
    Windows XP Access 2007
    Join Date
    Oct 2011
    Posts
    40
    Thanks mate for your suggestion
    mid function looks good for me but the problem is all name does not start with the same length.
    for e.g.
    "3pc pen blue" "3pieces pen green". Here as you can see if I put mid(columname,5,3)..it will not work for the second item...please help..

  4. #4
    hertfordkc is offline 18 year novice
    Windows XP Access 2007
    Join Date
    Mar 2011
    Posts
    481

    It gets messy, but it can be done.

    MyVal = Right(LngStrng,Len(LngStrng)-Instr(1,Lngstrng," ")
    That should return everything after the first blank.
    MyVal = Left(MyVal,Instr(1,MyVal," ")
    Should remove the right blank and following characters.

    However, since you are wanting to do this in (a calculated field in) a query, doing the above would require two queries where only one is needed. So:

    MyVal:Mid(LngStrng,Instr(LngString," ",1)+1,Len(LngStrng)-Instr(LngStrng," ",Instr(LngStrng," ",1)+1))

    That is just my imagination. so be prepared to check it in pieces. I'll get on my Access PC if it doesn't work for you.
    Note that the above assumes that there are not any leading or trailing blanks and that the field is not empty (or null?), If that were the case, you would need to surround it all with an iif statement to cut off errors.

  5. #5
    learning_graccess is offline Advanced Beginner
    Windows XP Access 2007
    Join Date
    Oct 2011
    Posts
    40
    Thanks mate. It's working.

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

Similar Threads

  1. Align text in a field column.
    By israellawndale in forum Access
    Replies: 1
    Last Post: 08-16-2011, 06:18 PM
  2. pulling text values into FK number column
    By REBBROWN in forum Database Design
    Replies: 2
    Last Post: 08-30-2010, 05:04 PM
  3. Replies: 7
    Last Post: 04-27-2010, 02:47 PM
  4. Adding text to column if match
    By niihla10 in forum Access
    Replies: 0
    Last Post: 08-26-2009, 01:39 PM
  5. centre justify column text
    By marky in forum Access
    Replies: 0
    Last Post: 09-01-2008, 12:02 PM

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