Solved String without last Word

nirmal

Member
Local time
Today, 14:12
Joined
Feb 27, 2020
Messages
82
Sir,
I have a string field value in a column Place in Place_tbl, for example
123, abdf, sygea, dsead
or
kasr, dladr, ase, 4896

I want the result without the last word
123, abdf, sygea
kasr, dladr, ase

What should be the query design
Please Help
 
try

shortenedPlace: left(Place,instrrev(place,",")-1)

to test in the immediate window
left("kasr, dladr, ase, 4896",instrrev("kasr, dladr, ase, 4896",",")-1)
kasr, dladr, ase
 
try

shortenedPlace: left(Place,instrrev(place,",")-1)

to test in the immediate window
left("kasr, dladr, ase, 4896",instrrev("kasr, dladr, ase, 4896",",")-1)
kasr, dladr, ase
Thank You it works
 
@nirmal. Are any strings Null or have no ","? If so you may want a custom function

Code:
Public Function WithoutLast(TextIn As Variant) As Variant
  If Not IsNull(TextIn) Then
   WithoutLast = TextIn
   If InStr(TextIn, ",") > 0 Then
    WithoutLast = Left(TextIn, InStrRev(TextIn, ",") - 1)
   End If
  End If
End Function

or modify the in line function
Code:
 IIf(InStr([place],",")>0,Left(Nz([place]),InStrRev([place],",")-1),[place])
Comparing the two
Query1 Query1

Field1Expr1Expr2
the brown fox#Func!the brown fox
the, brown, foxthe, brownthe, brown
The#Func!The
the, foxthethe
#Error
 
Last edited:
The best solution is to fix the structure. It is poor practice to mush multiple fields into one since it violates first normal form.
 
@nirmal. Are any strings Null or have no ","? If so you may want a custom function

Code:
Public Function WithoutLast(TextIn As Variant) As Variant
  If Not IsNull(TextIn) Then
   WithoutLast = TextIn
   If InStr(TextIn, ",") > 0 Then
    WithoutLast = Left(TextIn, InStrRev(TextIn, ",") - 1)
   End If
  End If
End Function

or modify the in line function
Code:
 IIf(InStr([place],",")>0,Left(Nz([place]),InStrRev([place],",")-1),[place])
Comparing the two
Query1 Query1

Field1Expr1Expr2
the brown fox#Func!the brown fox
the, brown, foxthe, brownthe, brown
The#Func!The
the, foxthethe
#Error
Sir none string will be Null or there won't be a string without comma
 

Users who are viewing this thread

Back
Top Bottom