Solved

How to return the first letter of a string of letters and numberr

Posted on 2015-01-09
9
175 Views
Last Modified: 2015-01-09
I have a field that contains a string of numbers and letters that always has 1 or more numbers at the beginning. I need to pull only the first letter, from the left,  that is contained in the string.
0
Comment
Question by:EMCIT
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
  • +2
9 Comments
 
LVL 47

Expert Comment

by:Martin Liss
ID: 40540688
Dim lngIndex As Long

For lngIndex = 1 To Len(MyString)
    If Not IsNumeric(Mid(MyString, lngIndex, 1)) Then
        MsgBox Mid(MyString, lngIndex, 1)
        Exit For
    End If
Next

Open in new window

0
 
LVL 18

Accepted Solution

by:
Simon earned 500 total points
ID: 40540692
This works (in a query)

SELECT Left(Replace(Replace(Replace(Replace(Replace(Replace(Replace(Replace(Replace(Replace([Yourstring],'0',''),'1',''),'2',''),'3',''),'4',''),'5',''),'6',''),'7',''),'8',''),'9',''),1) AS Expr1
FROM Table1;

Open in new window


Yes, that really is ten levels of nested replace functions ;)

This should be faster than vba if you're processing the contents of 100s of rows. Obviously not so good for a single field value on a form.
0
 
LVL 26

Expert Comment

by:Nick67
ID: 40540706
Lots of different ways to do that.
A bunch of folks will chime in.
Pick the most efficient and elegant method suggested

Using VBA

Function ReturnFirstLetter(strData as string) as String
Dim Done as Boolean

Done = False
'Lets recursively select the first character of the string
Do until Done = True
    if Len(StrData) = 0 then
        ReturnFirstLetter ="No Letter!"
        Done = True
        Exit Do
    end if
    'Check if it's a number
    If isNumeric(Left(StrData,1)) = True
        ReturnFirstLetter = Left(StrData,1)
        Done = True
    else
        'trim off the leftmost character if it isn'y
        StrData = Right(StrData, Len(StrData)-1)
    end if
Loop

End Function

Open in new window

0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 120

Expert Comment

by:Rey Obrero (Capricorn1)
ID: 40540769
i think this simple query will do the job

select [F1], left(replace([F1],val([F1]),""),1) as FirstLetter
from tablex


.
0
 
LVL 50

Expert Comment

by:Gustav Brock
ID: 40540787
Use this minimal expression:

StrFirstChar = Mid(strNumberText, Len(CStr(Val("1" & strNumberText))), 1)

/gustav
0
 
LVL 26

Expert Comment

by:Nick67
ID: 40540794
@gustav
Some explanation of what that's doing would be nice :)

Nick67
0
 
LVL 120

Expert Comment

by:Rey Obrero (Capricorn1)
ID: 40540802
speaking of minimal,

see http:#a40540769
0
 
LVL 50

Expert Comment

by:Gustav Brock
ID: 40540823
Assuming that a value can have a leading zero:

    "01234abcd"

I prefix a "1" to avoid a leading zero:

    "101234abcd"

Now Val safely can extract the number part:

    101234

This is converted to a string:

    CStr(Val("1" & strNumberText))

which has a length:

    Len(CStr(Val("1" & strNumberText)))

which is used as the second parameter in Mid to define the position of the character:

    Mid(strNumberText, Len(CStr(Val("1" & strNumberText))))

Finally the last parameter is applied to extract one character only:

    Mid(strNumberText, Len(CStr(Val("1" & strNumberText))), 1)

Great fun!
Gustav
0
 
LVL 26

Expert Comment

by:Nick67
ID: 40540832
I had lost sight of the original parameters
Multiple numbers before letters.
Now Val() makes sense
Get the count of numbers from the front
Shear those off and take the first thing remaining.

or replace them with "" as @Rey did

Lots of ways to do it.
Didn't figure mine'd be the winner :)
0

Featured Post

SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Balance After Payment 12 58
CISCO WIFI 6 71
Return Data From Website in Access 6 51
Userform to show a range in excel. 3 33
Describes a method of obtaining an object variable to an already running instance of Microsoft Access so that it can be controlled via automation.
AutoNumbers should increment automatically, without duplicates.  But sometimes something goes wrong, and the next AutoNumber value is a duplicate.  This article shows how to recover from this problem.
Basics of query design. Shows you how to construct a simple query by adding tables, perform joins, defining output columns, perform sorting, and apply criteria.
In Microsoft Access, learn the trick to repeating sub-report headings at the top of each page. The problem with sub-reports and headings: Add a dummy group to the sub report using the expression =1: Set the “Repeat Section” property of the dummy…

739 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question