SQL Query: valid date

I have a TEXT field in my table that should contains dates in YYYYMMDD format but some may not be.

I need to check that they are in that format AND that it is a valid date.


How?
vbnetcoderAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Rey Obrero (Capricorn1)Commented:
place this codes in a regular module

function IsValidDate(varStr) as boolean
if len(varStr)> 8 then isvalidDate=false : exit function
if cint(left(varStr,4)) Between 1900 and 2099 then
   'do nothing
  else
  IsValidDate=False
  exit function
end if
if cint(mid(varStr,5,2)) Between 1 and 12 then
   'do nothing
  else
  IsValidDate=False
  exit function
end if
 
if cint(mid(varStr,7,2)) Between 1 and 31 then
   'do nothing
  else
  IsValidDate=False
  exit function
end if
IsValidDate=true

end function

 
now create a query

select [dateField], IsValidDate([datefield]) As TrueDate
from table

the values with zero on TruDate are not valid date
Rey Obrero (Capricorn1)Commented:
sctracth that earlier post

use this one


Function IsValidDate(varStr) As Boolean
If Len(varStr) > 8 Then IsValidDate = False: Exit Function
IsValidDate = IsDate(Left(varStr, 4) & "/" & Mid(varStr, 5, 2) & "/" & Mid(varStr, 7, 2))
End Function

Open in new window

vbnetcoderAuthor Commented:
Rey,

for my purposes i need to do that as an access sql query...
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

Rey Obrero (Capricorn1)Commented:
you can use the codes in the query

select [dateField], IsValidDate([datefield]) As TrueDate
from table

or something like this in a query

SELECT dateField, IsDate(Left([dateField],4) & "/" & Mid([dateField],5,2) & "/" & Mid([dateField],7,2)) AS Expr1
FROM Table1;
vbnetcoderAuthor Commented:
How would i write it so that this logic was in the where clause?  Or to be more precise i want to return all the values where it is NOT a valid date

 IsDate(Left([dateField],4) & "/" & Mid([dateField],5,2) & "/" & Mid([dateField],7,2))
Rey Obrero (Capricorn1)Commented:
in the criteria of the column of the expression, place 0

field:       IsDate(Left([dateField],4) & "/" & Mid([dateField],5,2) & "/" & Mid([dateField],7,2))
table

criteria  0
Gustav BrockCIOCommented:
It is much easier to use Format:

Select * From YourTable Where Not IsDate(Format([dateField], "@@@@-@@-@@"))

/gustav

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
vbnetcoderAuthor Commented:
ty
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Access

From novice to tech pro — start learning today.