Solved

Format of date stored as text compared to date() => problem with month

Posted on 2014-01-29
4
433 Views
Last Modified: 2014-01-29
I'm using format to arrange a date time stored as text:

Format(Mid([CUSTOM_FIELD],5,2) & "/" & Mid([CUSTOM_FIELD],7,2) & "/" & Left([CUSTOM_FIELD],4),"mm/dd/yyyy")

so that I can then use date() to work out data from yesterday date()-1

The trick is that my formating creates a month as 2 digits always and date() will return the month as a single digit creating a situation where 01/28/2014 and 1/28/2014 are not recognized as the same.

I'm wondering what I can do with this to correct for this:

WHERE (((Format(Mid([CUSTOM_FIELD],5,2) & "/" & Mid([CUSTOM_FIELD],7,2) & "/" & Left([CUSTOM_FIELD],4),"mm/dd/yyyy"))=Date()-1));
0
Comment
Question by:ghettocounselor
  • 2
4 Comments
 
LVL 61

Accepted Solution

by:
mbizup earned 500 total points
ID: 39817826
Try this (compares both sides using the same format):

WHERE (((Format(Mid([CUSTOM_FIELD],5,2) & "/" & Mid([CUSTOM_FIELD],7,2) & "/" & Left([CUSTOM_FIELD],4),"mm/dd/yyyy"))=Format(Date()-1),"mm/dd/yyyy)");

Open in new window



Or if your text field is a standard/valid date format:

WHERE CDate([CUSTOM_FIELD])  = dateadd("d", -1, Date())
0
 

Author Closing Comment

by:ghettocounselor
ID: 39817850
Sweet, thanks for quick response.
Notes:
Needed one more paren after format
Format((Date()-1),"mm/dd/yyyy")
0
 
LVL 32

Expert Comment

by:awking00
ID: 39817876
It appears that your "dates" are stored as text in a 'yyyymmdd' format, so it might be easier to convert date() - 1 to text to do the compare. This only works for that format since any comparison will be based on ascii values.
where custom_field = format(date() - 1,"yyyymmdd")

A good example why you should NEVER store dates as anything other than date/time datatypes if you need to perform any kind of data math.
0
 
LVL 32

Expert Comment

by:awking00
ID: 39817890
Be careful! ASCII comparisons can produce some unwanted results. For example,
01/15/2014 will come before 02/15/2013. The reason I said you can get away with yyyymmdd formats.
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Confronted with some SQL you don't know can be a daunting task. It can be even more daunting if that SQL carries some of the old secret codes used in the Ye Olde query syntax, such as: (+)     as used in Oracle;     *=     =*    as used in Sybase …
Article by: Leon
Software Metering within our group of companies has always been an afterthought until auditing of software and licensing became a pain point. Orchestrator and SCCM metering gave us the answer and it was an exciting process.
In Microsoft Access, learn how to “cascade” or have the displayed data of one combo control depend upon what’s entered in another. Base the dependent combo on a query for its row source: Add a reference to the first combo on the form as criteria i…
The view will learn how to download and install SIMTOOLS and FORMLIST into Excel, how to use SIMTOOLS to generate a Monte Carlo simulation of 30 sales calls, and how to calculate the conditional probability based on the results of the Monte Carlo …

896 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now