We help IT Professionals succeed at work.

PATINDEX to retrieve date from varchar/memo data field

larrybye
larrybye asked
on
1,670 Views
Last Modified: 2008-01-09
I need to retrieve the date from a varchar/memo field, it could be formatted either mm/dd/yy or mm/dd/yyyy. I’d like to use PATINDEX as part of a function but unable to get the “pattern” correct for the date.

Data row Examples:
Qualify the unit for sale in the European community.  CANCELLED 5/14/97 See test AG20 on ED150.
All components are the same as the ED 150 5/16/1997.




Comment
Watch Question

Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION

Author

Commented:
Thanks, that is giving me the stating positions of the date in the field but I still need to return the actual date. I was hoping that I could modify the following function which returns numbers and “/” with a correct pattern and it would display the date but it returns the entire field.

create function dbo.fnNumbersFromStr(@str varchar(8000))
returns varchar(8000)
as
begin
      while patindex('%[^0-9/]%',@str)>0
            Set @str = rtrim(ltrim(replace(@str,substring(@str,patindex('%[^0-9/]%',@str),1),'')))
      return @str
end
go


What you’ve given me has gotten me closer, thanks again.

Commented:
Do you want to just return the first date or all of them? If all of them, should they be comma delimited or..?

Author

Commented:
Just the first date

Commented:
This should do it - I haven't tested it so it may need a little tweaking to get it right

create function dbo.fnNumbersFromStr(@str varchar(8000))
returns varchar(8000)
as
begin

      DECLARE @pos INT
      DECLARE @firstpos INT
      DECLARE @strlen INT

      SET @firstpos=PATINDEX('%[1-9]/[0-9][0-9]/[0-9][0-9]%',@str)
      SET @endpos=@firstpos+7

      SET @pos=PATINDEX('%[1-9]/[1-9]/[0-9][0-9]%',@str)
      IF @pos<@firstpos
            BEGIN
                  SET @firstpos=@pos
                  SET @strlen=6
            END

      SET @pos=PATINDEX('%[1][0-9]/[0-9][0-9]/[0-9][0-9]%',@str)
      IF @pos<@firstpos
            BEGIN
                  SET @firstpos=@pos
                  SET @strlen=8
            END

      SET @pos=PATINDEX('%[1][0-9]/[1-9]/[0-9][0-9]%',@str)
      IF @pos<@firstpos
            BEGIN
                  SET @firstpos=@pos
                  SET @strlen=7
            END

      SET @pos=PATINDEX('%[1-9]/[0-9][0-9]/[12][0-9][0-9][0-9]%',@str)
      IF @pos<@firstpos
            BEGIN
                  SET @firstpos=@pos
                  SET @strlen=9
            END

      SET @pos=PATINDEX('%[1-9]/[1-9]/[12][0-9][0-9][0-9]%',@str)
      IF @pos<@firstpos
            BEGIN
                  SET @firstpos=@pos
                  SET @strlen=8
            END

      SET @pos=PATINDEX('%[1][0-9]/[0-9][0-9]/[12][0-9][0-9][0-9]%',@str)
      IF @pos<@firstpos
            BEGIN
                  SET @firstpos=@pos
                  SET @strlen=10
            END

      SET @pos=PATINDEX('%[1][0-9]/[1-9]/[12][0-9][0-9][0-9]%',@str)
      IF @pos<@firstpos
            BEGIN
                  SET @firstpos=@pos
                  SET @strlen=9
            END
     
      IF @firstpos>0
            RETURN substring(@str,@firstpos,@strlen)
     

end
go

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.