Solved String without last Word (1 Viewer)

nirmal

Member
Local time
Tomorrow, 03:47
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
 

CJ_London

Super Moderator
Staff member
Local time
Today, 23:17
Joined
Feb 19, 2013
Messages
16,618
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
 

nirmal

Member
Local time
Tomorrow, 03:47
Joined
Feb 27, 2020
Messages
82
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
 

MajP

You've got your good things, and you've got mine.
Local time
Today, 18:17
Joined
May 21, 2018
Messages
8,535
@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:

Pat Hartman

Super Moderator
Staff member
Local time
Today, 18:17
Joined
Feb 19, 2002
Messages
43,302
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

Member
Local time
Tomorrow, 03:47
Joined
Feb 27, 2020
Messages
82
@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

Top Bottom