Extracting a date from a string field

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Extracting a date from a string field

Nandini Rao
My data has the date embedded in a string field (A17) in the following two
formats m/d/yyyy h:mm:ss and mm/d/yyyy h:mm:ss

What is the best way to extract just the mm/d/yyyy field from the string?

Thanks for your help!
Reply | Threaded
Open this post in threaded view
|

Re: Extracting a date from a string field

Richard Ristow
At 04:42 PM 11/27/2006, nan wrote:

>My data has the date embedded in a string field (A17) in the following
>two
>formats m/d/yyyy h:mm:ss and mm/d/yyyy h:mm:ss
>
>What is the best way to extract just the mm/d/yyyy field from the
>string?

It depends on how much variation there is in your string formats. If
the only possibilities are the following (where "." character represent
a space),

m/d/yyyy.h:mm:ss
.m/d/yyyy.h:mm:ss
mm/d/yyyy.h:mm:ss

and the variable is named CHAR_DT, then

SUBSTR(CHAR_DT,1,9)

will do it. That will also work with any number of additional spaces
between the date and time portion.

THIS LOGIC DOES NOT CHECK FOR UNEXPECTED VARIATIONS IN THE INPUT
FORMAT.

If there's greater variation, for example a varying number of spaces
before the date portion, you need more complex logic, but what you need
depends on what variations in format you have.

(If the only variation is varying number of spaces before the date,
then

SUBSTR(LTRIM(CHAR_DT),1,9)

will do it.)

This is intentionally a very quick answer. If your variations in data
format are too complex for these expressions, post again with some
examples.