Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Trimming leading & trailing text in a query 2

Status
Not open for further replies.

bigchuck

Programmer
Oct 10, 2002
14
US
I am looking for the right function(s) to trim a constant number of leading and trailing characters off of a text field of varying length.
Example:
I start with these strings:
123_abcdefg_789
456_zyxwvu_987

I want these results:
abcdefg
zyxwvu

I can get the first four characters off using a mid function; however, it's the trailing characters that are giving me fits.
Any ideas?
 
This should work:

?mid("123_abcdefg_789",instr("123_abcdefg_789","_")+1,instrrev("123_abcdefg_789","_")-instr("123_abcdefg_789","_")-1)

Or in your query:

select mytext
,mid([mytext],instr([mytext],"_")+1,instrrev([mytext],"_")-instr([mytext],"_")-1) as strip_text
from mytable


Mike Pastore

Hats off to (Roy) Harper
 
I think I got it. I wrote the following function

Public Function excise(strField As String, intStartTrim As Integer, intEndtrim As Integer)
'Function to trim a constant amount from the beginning and end from a string
'intStartTrim is the number of characters to be trimmed from the beggining
'intEndTrim is the number of characters to be trimmed from the end

Dim intLength As Integer

'intLength is the total length of the variable length string minus the trimmed lengths
intLength = Len(strField) - intEndtrim - intStartTrim

'Now the function is reduced to using a mid function using the above variables as arguments
excise = Mid(strField, intStartTrim, intLength)
End Function
 
I am wanting to trim leading and trailing spaces, vbProperCase, and remove double spaces in the string, all in a query.

Is that possible?

My question is posted at thread701-584237

Thanks!

"The potentialities of a language are so vast that we must wonder sometimes at ourselves: how do we manage to know so much?" Anthony Burgess, A Mouthful of Air
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top