Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Parsing a text string 1

Status
Not open for further replies.

chellweg

IS-IT--Management
Dec 8, 2003
20
US
i am retrieving data from a field in my database, but want to strip off the text portion at the end. The first half is dates, and that is all I care about. An example of one of the strings is:

02-18-2005 - 02-19-2005 - Rale

All I want to use out of it is:

02-18-2005 - 02-19-2005

What is the easiest way to extract the date portion of the field to use.
 
Assuming that your dates are always the same number of characters you can just take the leftmost 23 characters.

02-18-2005 - 02-19-2005
12345678901234567890123

So like this:
MyNewValue = Left(MyOldValue, 23)
 
well that would have looked better if i put it in a code tag so the letter will line up in courier font.

Code:
02-18-2005 - 02-19-2005
12345678901234567890123

Ok, now it makes more sense.
 
Hi chellweg,


Since it is a string data and not a date try manipulating it using the Left function


' only include the first 23 char of this string
' you need to assign it to a variable to work
<%

strDate = Left("02-18-2005 - 02-19-2005 - Rale",23)

response.write strDate
' result 02-18-2005 - 02-19-2005


Hope this is what is you needed if not please elaborate more

BSL

%>
 
I like yours better anyway where you actually used his value instead of silly variable like MyNewValue and MyOldValue.
 
Hi,
Just offering an alternative method which I think may be more 'flexible'.
------
dim vsString, vasParts
dim vdDate1, vdDate2

vsString = "02-18-2005 - 02-19-2005 - Rale"
vasParts = split( vsString, " - " )

vsDate1 = vasParts(0)
vsDate2 = vasParts(1)
------
However, it will be better to use the , or ; as separator instead of - as the - is also separators within the date fields.

Just an alternative perspective and can be useful if the format/length of the vsString cannot be determined (ie, 2-18-05 instead of 02-18-2005).

regards,
- Joseph
==============

~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-
 
My suggestion would be to just strip off everything after the last dash, allows flexibility and cheaper then creating an array:
Code:
Dim myVar 'heh
myVar = "02-18-2005 - 02-19-2005 - Rale"

'alter it to drop right side
myVar = Left(myVar,InStrRev(myVar,"-") - 1)

Additionally, to polish up BSL's, you could split on " - " (space-dash-space in case it's hard to read). That wshould then give you an array of three elements and you can safely play with the first two seperatly (bonnus).

-T

barcode_1.gif
 
regex hehe :p

might degrade perfomance for this task though

___________________________________________________________________

onpnt.com
SELECT * FROM programmers WHERE clue > 0
(0 row(s) affected) -->faq333-3811

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top