Solved How do I remove a specific character at it's last occurrence in a string, from a query? (1 Viewer)

Sampoline

Member
Local time
Today, 18:20
Joined
Oct 19, 2020
Messages
161
I want to be able to remove the last occurrence of "/" from data I have in a query.. like below:

Original Data Example:
X/Y/A1/
X/Y/B1/
X/Y/B2/
X/Y/B4/
X/Y/B7a
X/Y/B8a
X/Y/B3/
X/Y/B3/
X/Y/B9a
X/Y/B4/

Required Data Example:
X/Y/A1
X/Y/B1
X/Y/B2
X/Y/B4
X/Y/B7a
X/Y/B8a
X/Y/B3
X/Y/B3
X/Y/B9a
X/Y/B4

What sort of expression should I use? I've tried doing a few Replace expressions, but couldn't get it right. Any help would be appreciated. Thanks.
 

Isaac

Lifelong Learner
Local time
Today, 01:20
Joined
Mar 14, 2017
Messages
8,777
If X/Y/B7a becomes X/Y/B7a, then you don't want to remove the last occurrence of /
 

Sampoline

Member
Local time
Today, 18:20
Joined
Oct 19, 2020
Messages
161
If X/Y/B7a becomes X/Y/B7a, then you don't want to remove the last occurrence of /
Yep that's my confusion now. So how can I only remove the ones that are reflected as so on my list?
 

MajP

You've got your good things, and you've got mine.
Local time
Today, 04:20
Joined
May 21, 2018
Messages
8,527
You can do with iif, here it is with a UDF
Code:
Public Function RemoveSlash(varText As Variant) As String
  If Not IsNull(varText) Then
    If Right(varText, 1) = "/" Then varText = Left(varText, Len(varText) - 1)
    RemoveSlash = varText
  End If
 End Function
 

theDBguy

I’m here to help
Staff member
Local time
Today, 01:20
Joined
Oct 29, 2018
Messages
21,467
Maybe something like
Code:
IIf(Right([FieldName],1)="\",Left([FieldName],Len(FieldName)-1),[FieldName])

LOL. Too slow! (using my phone...)
 

Isaac

Lifelong Learner
Local time
Today, 01:20
Joined
Mar 14, 2017
Messages
8,777
Yep that's my confusion now. So how can I only remove the ones that are reflected as so on my list?
Well it's even more confusing if you mis-state your requirement, or when your stated requirement vs. your sample data represent a contradiction.

I think what you want to say is: If the last character is /, then I want to remove /, otherwise leave the original value intact.
 

Sampoline

Member
Local time
Today, 18:20
Joined
Oct 19, 2020
Messages
161
You can do with iif, here it is with a UDF
Code:
Public Function RemoveSlash(varText As Variant) As String
  If Not IsNull(varText) Then
    If Right(varText, 1) = "/" Then varText = Left(varText, Len(varText) - 1)
    RemoveSlash = varText
  End If
End Function
Maybe something like
Code:
IIf(Right([FieldName],1)="\",Left([FieldName],Len(FieldName)-1),[FieldName])

LOL. Too slow! (using my phone...)
Thank you everyone for your help!

I hadn't explored the function for it, this is very useful for another issue I had with another project, so thank you for this solution.

I didn't know about IIf in the query DB, thanks for that. This worked best for my current issue.
 

Sampoline

Member
Local time
Today, 18:20
Joined
Oct 19, 2020
Messages
161
Well it's even more confusing if you mis-state your requirement, or when your stated requirement vs. your sample data represent a contradiction.

I think what you want to say is: If the last character is /, then I want to remove /, otherwise leave the original value intact.
My apologies, thankyou for the correction. I'll be a bit more careful next time.
 

theDBguy

I’m here to help
Staff member
Local time
Today, 01:20
Joined
Oct 29, 2018
Messages
21,467
Thank you everyone for your help!

I hadn't explored the function for it, this is very useful for another issue I had with another project, so thank you for this solution.

I didn't know about IIf in the query DB, thanks for that. This worked best for my current issue.
Hi. Glad to hear you got it sorted out. Good luck with your project.
 

Users who are viewing this thread

Top Bottom