473,395 Members | 1,466 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Trying to strip out specific text from a field

219 100+
I've got a field in my database, and I want to retrieve all characters to the left of the first number value encountered. I'm not quite sure how to accomplish this

Here is an example of a field I'm working with:

PREDNISONE 10 MG = 2 TAB (10 MG = 2 TAB TAB)

I want to retrieve the value:
PREDNISONE

Any ideas?
Apr 2 '08 #1
2 1827
Stewart Ross
2,545 Expert Mod 2GB
Hi. There s no native function that I know of which can do what you want, but it is easy to devise a custom function in VB which does.

Copy the function below into a public code module (select Modules from the database window and open any existing module). If you do not have any existing modules create a new one, paste the function in, and save the module under any suitable name.

Expand|Select|Wrap|Line Numbers
  1. Public Function LeftmostChars(inputstring As String) As String
  2.     'Finds the first position in the inputstring
  3.     'of a numeric character and returns all characters
  4.     'to the left of that position, with spaces trimmed on right
  5.     '
  6.     Dim PosFound As Integer
  7.     Dim NumbertoCheck As Integer
  8.     NumbertoCheck = 0
  9.     Do
  10.         PosFound = InStr(1, inputstring, CStr(NumbertoCheck))
  11.         NumbertoCheck = NumbertoCheck + 1
  12.     Loop Until (NumbertoCheck > 9) Or (PosFound > 0)
  13.     If PosFound = 0 Then
  14.         LeftmostChars = inputstring
  15.     Else
  16.         LeftmostChars = RTrim(Left$(inputstring, PosFound - 1))
  17.     End If
  18. End Function
To use it in a query, add a new column to the query and create a calculated field as in the example below, replacing the field name passed to the function the actual name of your field:

Drugname: LeftmostChars([Prescribed Dose])

No doubt there are simpler solutions than what I have come up with, but it was quick to do and it works...

-Stewart
Apr 2 '08 #2
ADezii
8,834 Expert 8TB
I've got a field in my database, and I want to retrieve all characters to the left of the first number value encountered. I'm not quite sure how to accomplish this

Here is an example of a field I'm working with:

PREDNISONE 10 MG = 2 TAB (10 MG = 2 TAB TAB)

I want to retrieve the value:
PREDNISONE

Any ideas?
Just a different approach:
Expand|Select|Wrap|Line Numbers
  1. Public Function fRetrieveCharsBeforeNumber(strValue As String) As String
  2. Dim intNumOfChars As Integer
  3. Dim intCounter As Integer
  4.  
  5. If Len(strValue) = 0 Then Exit Function
  6.  
  7. intNumOfChars = Len(strValue)
  8.  
  9. For intCounter = 1 To intNumOfChars
  10.   If IsNumeric(Mid$(strValue, intCounter, 1)) Then
  11.     fRetrieveCharsBeforeNumber = Left$(strValue, intCounter - 1)
  12.       Exit For
  13.   Else
  14.     fRetrieveCharsBeforeNumber = vbNullString
  15.   End If
  16. Next
  17. End Function
Apr 2 '08 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: Mark Miller | last post by:
I have a scheduled job that uses different XSL templates to transform XML and save it to disk. I am having problems with the code below. The problem shows up on both my development machine (Windows...
1
by: Del | last post by:
We have a MS SQL Server 2000 application for our shipping system. We have created a front end application using MS Access for our order analyst. The front end application allows the analyst to...
0
by: Brian Henry | last post by:
I thought this was useful, its a extender i just wrote for the new .NET menu strip and status strip to extend the menu items to add a status message so when a mouse rolls over the item it displays...
2
by: tshad | last post by:
Is there an easy way to strip HTML tags from Text to get just the plain text? I am using a program called FreeTextBox that lets you format Text in a TextBox. It does this by adding HTML tags...
2
by: Cruella DeVille | last post by:
I must have som errors in my understanding of strip- vs addslashes. I thought that if a user submitted eg a username, like this username=siv' drop database test; I should addslashes to escape ' and...
4
by: Steve | last post by:
Hi, I'm a complete PHP n00b slowly finding my way around I'm using the following function that I found on php.net to strip out html and return only the text. It works well except for when you...
2
by: Andy_Khosravi | last post by:
I have a form with a large memo field that is designed to hold lengthy comments. There are times when my users need to paste in e-mails directly into the comment box. This works fine unless their...
3
by: Drum2001 | last post by:
Hello, I have a textbox "Fname" where users input what they would like a filename to be. I would like to strip out all invalid characters with an "After Update" Event. I have searched other...
2
by: Pete | last post by:
I need to create a single query (Not a SQL query) against a single table that counts the number of records in the table, where the single field "tmp" contains specific string values If the field...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.