10

I have a string with the following format:

StockCode Country Date Price equity

for example:

  • 1 hk 10/31/12 C70.5 equity
  • 101 hk 11/21/13 P63 equity
  • 388 hk 10/17/12 P100 equity

I can extract Date by this Excel Command:

LEFT(RIGHT(RIGHT(LEFT(A1,LEN(A1)-7),LEN(LEFT(A1,LEN(A1)-7))-FIND(" ",A1)),
LEN(RIGHT(LEFT(A1,LEN(A1)-7),LEN(LEFT(A1,LEN(A1)-7))-FIND(" ",A1)))-FIND(" ",
RIGHT(LEFT(A1,LEN(A1)-7),LEN(LEFT(A1,LEN(A1)-7))-FIND(" ",A1)))),FIND(" ",
RIGHT(RIGHT(LEFT(A1,LEN(A1)-7),LEN(LEFT(A1,LEN(A1)-7))-FIND(" ",A1)),
LEN(RIGHT(LEFT(A1,LEN(A1)-7),LEN(LEFT(A1,LEN(A1)-7))-FIND(" ",A1)))-FIND(" ",
RIGHT(LEFT(A1,LEN(A1)-7),LEN(LEFT(A1,LEN(A1)-7))-FIND(" ",A1))))))

(line breaks for readbility)

result: 10/31/12

Anyone has a better solution for this?

here are some of my codes:

  • how to get "C" and "P":

    TRIM(MID(A1,IFERROR(FIND("P",A1),FIND("C",A1)),1))
    
  • how to get the strike price:

    TRIM(SUBSTITUTE(RIGHT(A1,LEN(A1)-IFERROR(FIND("P",A1),
    FIND("C",A1))),"equity",""))
    

7 Answers 7

14

Try this, I think it should work for all dates with a 2 character year:

=TRIM(MID(A1,FIND("/",A1)-2,8))
Sign up to request clarification or add additional context in comments.

2 Comments

This also assumes that both month and day are also always two characters (i.e. all dates are mm/dd/yy), and that StockCode and Country will never contain a "/".
Thanks John, Finally DATEVALUE(Date_type_like_TEXT) is requited to change the extracted string to be date, eg, mm/dd/yyyy
4

I'd recommend running Text to Columns from the Data tab. Choose Delimited, and indicate space as the delimiter. This will put your string into five columns with the third one being date, and then you can freely use the data directly.

Otherwise, I'd look at Lajos Example:( =TRIM(MID(A1,FIND("!^!",SUBSTITUTE(A1," ","!^!",2)),8)) ) or John's answer.

Comments

4

If you can use a macro UDF this is simple.

Function STR_SPLIT(str as String, sep as String, n as Long)
    Dim Arr() as String
    Arr = Split(str, sep)
    STR_SPLIT = Arr(n - 1)
End Function

str is the string to split, sep is the character to split on - " " in your case - and n is the column number you're looking for. So =STR_SPLIT(A1," ",3) would return the date for example.

Comments

3

You could split each part in to columns in Excel by going to Data > Text to Columns and using the space as a delimiter.

1 Comment

Solved a random issue of mine. Great recommendation.
1

Your date is between the second and the third space in all your examples. You can replace your third space with "!^!" for example and then get the substring until the first occurrence of "!^!". After that you just have to extract the substring after the second space. Kindly read more here.

Comments

1

This formula should also give the required result, even if year has 4 characters

=TRIM(MID(SUBSTITUTE(A1," ",REPT(" ",99)),198,99))

4 Comments

This would also work. =TRIM(MID(SUBSTITUTE(A1," ",REPT(" ",10000)),15000,10000)) +1 for providing a silly yet valid answer.
Thanks for the +1 Daniel but not sure why you feel it's "silly" - in what way?
Because it depends on stuffing the original string with lots of whitespace and then retrieving the desired information imprecisely. Granted, it's probably not silly, I suppose novel would be more appropriate. Sorry. Not that this matters, but your formula only returns the first 98 characters of the third term (Assuming the first and second term are 0 length). Less the total of the lenght of the first and second term. If the first and second term combined were more than than 97 characters long, the third term will not be returned at all because you aren't using enough whitespace filler.
I concede it was tailored based on the example strings provided, perhaps it would be better as =TRIM(MID(SUBSTITUTE(A1," ",REPT(" ",LEN(A1))),LEN(A1)*2,LEN(A1))) the 2 can be changed depending on which text one wants to retrieve
0

Thanks John

here are some of my codes:

how to get "C" and "P":

TRIM(MID(A1,IFERROR(FIND("P",A1),FIND("C",A1)),1))

how to get the strike price:

TRIM(SUBSTITUTE(RIGHT(A1,LEN(A1)-IFERROR(FIND("P",A1),FIND("C",A1))),"equity",""))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.