How to truncate a string in Access
September 20, 2006 9:15 AM Subscribe
I'm writing an query in MS Access to get column 1 = product ID, column 2 = product Description. However, the description field has addtional spec data that I don't need. I want to remove anything after the first word, meaning remove after the first blank space.
Example:
Prod ID Prod Desc
10-362-3 Packing DT
12-345-5 Seal XT L-23
Desired result:
10-362-3 Packing
12-345-5 Seal
I tried to use FIND or SEARCH functions to identify the position of the first blank space and to truncate everything after that. However, Access does not recoganize either FIND or SEARCH. Does anyone have any idea how to accomplish this? Thanks.
Yes, you will - I just modeled your sample data in Access and this did what you want:
posted by pocams at 9:21 AM on September 20, 2006
UPDATE products SET ProdDesc = LEFT(ProdDesc, INSTR(ProdDesc, ' ') - 1)
posted by pocams at 9:21 AM on September 20, 2006
Response by poster: Thanks, pocams. I tried INSTR, and it worked. But I have another issue here - some description strings only have one word. INSTR will erase the whole string in this case. I guess I can use a conditional check to get around that.
posted by dy at 10:23 AM on September 20, 2006
posted by dy at 10:23 AM on September 20, 2006
Yup, just a WHERE clause.
posted by pocams at 7:55 PM on September 20, 2006
UPDATE .... WHERE INSTR(ProdDesc, ' ') > 0
posted by pocams at 7:55 PM on September 20, 2006
This thread is closed to new comments.
UPDATE products SET ProdDesc = LEFT(ProdDesc, INSTR(ProdDesc, ' '))
You may have to use INSTR(ProdDesc, ' ') - 1 to remove the space from the column - I don't have Access handy to check right now.
posted by pocams at 9:18 AM on September 20, 2006