Page 1 of 2 12 LastLast
Results 1 to 15 of 16
  1. #1
    McArthurGDM is offline Competent Performer
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jul 2014
    Posts
    215

    Convert String to Date in Query

    I have a string value that comes out of a query in the form of



    01012015
    02012015
    12012014

    etc...

    where the first 2 digits are the month, the next 2 are the day, and the next 4 are the year.

    How do I convert these values to a valid date that can be queried on?

    Thanks!

  2. #2
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,965
    With string manipulation and date conversion functions.

    CDate(Left(x, 2) & "/" & Mid(x, 3, 2) & "/" & Right(x, 4))

    Or

    DateSerial(Right(x,4), Left(x,2), Mid(x,3,2))


    Why does query generate a string?
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  3. #3
    McArthurGDM is offline Competent Performer
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jul 2014
    Posts
    215
    I get a strange result when I do the calculation

    SELECT SearchReportQuarterlyCYAvg1.Quarter, SearchReportQuarterlyCYAvg1.MetricID, SearchReportQuarterlyCYAvg1.OE, SearchReportQuarterlyCYAvg1.AvgOfValues, SearchReportQuarterlyCYAvg1.Years, SearchReportQuarterlyCYAvg1.CountOfValues, Format(CDate(Left([NewDate],2) & "/" & Mid([NewDate],3,2) & "/" & Right([NewDate],4)),"Short Date") AS DateConvertFROM SearchReportQuarterlyCYAvg1
    WHERE (((Format(CDate(Left([NewDate],2) & "/" & Mid([NewDate],3,2) & "/" & Right([NewDate],4)),"Short Date"))<=#4/1/2015#));
    This is my original table of values after I converted.

    Quarter MetricID OE AvgOfValues Years CountOfValues DateConvert
    Q1 A1 2462559.66666667 2015 3 3/1/2015
    Q2 A1 2407390 2015 2 6/1/2015
    Q3 A1
    2015 0 9/1/2015
    Q4 A1
    2015 0 12/1/2015


    Then I run the criteria and this is the result

    Quarter MetricID OE AvgOfValues Years CountOfValues DateConvert
    Q1 A1 2462559.66666667 2015 3 3/1/2015
    Q4 A1
    2015 0 12/1/2015

  4. #4
    pbaldy's Avatar
    pbaldy is online now Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,530
    Don't use the Format() function, it changes the result to a string.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  5. #5
    IrogSinta is offline Competent Performer
    Windows 8 Access 2013
    Join Date
    Jun 2015
    Posts
    103
    Here's another way to convert your string to a date?
    CDate(Format([NewDate],"00/00/0000"))

    Ron

  6. #6
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,965
    Yep, that works, too.

    Still don't know why you have that oddball string to begin with.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  7. #7
    McArthurGDM is offline Competent Performer
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jul 2014
    Posts
    215
    Fair question, June. There probably was a better way to do what I was doing, but I had been working on it for almost 10 hours straight yesterday and just did was seemed doable rather than logical.

    Thanks for the help.


    You were right, Pbaldy, about it converting it back to a string. Shouldn't that not be the case? I thought the whole point of the format function was to convert it to a string type?

  8. #8
    IrogSinta is offline Competent Performer
    Windows 8 Access 2013
    Join Date
    Jun 2015
    Posts
    103
    You cannot do a comparison between String type and Date type in your WHERE clause. Not sure why you want to convert it to a String anyway. I agree with June, why not fix this at the source?

    Ron

  9. #9
    McArthurGDM is offline Competent Performer
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jul 2014
    Posts
    215
    I still get the same result when I take away the FORMAT function.

    I don't understand why this isn't working, given that it has been converted into a date. In fact, no matter what method I use to convert it to a date, I still get the same oddball results.

    EDIT:

    Actually, if I put in a Date in the criteria like #3/1/2015#. It works.

    When I reference a form for the date, that's when I get the wacky results.

    Why is that?

  10. #10
    pbaldy's Avatar
    pbaldy is online now Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,530
    Can you post the db here?
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  11. #11
    McArthurGDM is offline Competent Performer
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jul 2014
    Posts
    215
    Wish I could.

    Database contains company-sensitive information.

    The issue lies in the fact that I am referencing a report control for the date.

    When I input a date and put the hashtags around it, it works just fine. I think it's not actually assuming that the control on the form is a date, even though I designated the control as such.

    I tried putting hashtags around the report reference, that is #Reports!SearchReport!DateControl#

    But that dosen't work and says "Not a valid date"

  12. #12
    pbaldy's Avatar
    pbaldy is online now Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,530
    No, you don't want those (only for a literal date). Try

    WHERE CDate(Left([NewDate],2) & "/" & Mid([NewDate],3,2) & "/" & Right([NewDate],4))<=Reports!SearchReport!DateControl
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  13. #13
    McArthurGDM is offline Competent Performer
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jul 2014
    Posts
    215
    I think that's pretty much what I already have that's not working.

    I did some tests and I discovered that the only date value that's giving me weird results is the 12/1/2015 date

    Here's the code I used to create the new dates:

    NewDate: IIf([Months].[Quarter]="Q1","0301" & [Reports]![SearchReport]![SearchReportYear],IIf([Months].[Quarter]="Q2","0601" & [Reports]![SearchReport]![SearchReportYear],IIf([Months].[Quarter]="Q3","0901" & [Reports]![SearchReport]![SearchReportYear],IIf([Months].[Quarter]="Q4","1201" & [Reports]![SearchReport]![SearchReportYear]))))

    And the query as I stated:

    SELECT SearchReportQuarterlyCYAvg1.Quarter, SearchReportQuarterlyCYAvg1.MetricID, SearchReportQuarterlyCYAvg1.OE, SearchReportQuarterlyCYAvg1.AvgOfValues, SearchReportQuarterlyCYAvg1.Years, SearchReportQuarterlyCYAvg1.CountOfValues, SearchReportQuarterlyCYAvg1.NewDate, CDate(Format([NewDate],"00\/00\/0000")) AS DateCovert
    FROM SearchReportQuarterlyCYAvg1
    WHERE (((CDate(Format([NewDate],"00\/00\/0000")))<=[Reports]![SearchReport]![ReportMonthYear]));

  14. #14
    pbaldy's Avatar
    pbaldy is online now Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,530
    I suppose you can try wrapping the report reference in the CDate() function as well, to make sure it's interpreted correctly as a date. The problem with 12/1 is indicative of a string comparison.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  15. #15
    McArthurGDM is offline Competent Performer
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jul 2014
    Posts
    215
    It worked.

    Thank you!

    I had to wrap the Form reference in the CDate function.

    For whatever reason, it wasn't reading it as an actual date. I will have to remember that for the future.

    Why is it that the problems that drive us nuts often have the simplest solutions?

Page 1 of 2 12 LastLast
Please reply to this thread with any new information or opinions.

Similar Threads

  1. Convert String to Date in Web App
    By carlo76 in forum SharePoint
    Replies: 3
    Last Post: 03-06-2015, 11:05 AM
  2. Convert Text String to Date in SQL
    By kestefon in forum Access
    Replies: 2
    Last Post: 12-04-2013, 03:33 PM
  3. How to convert Date to String?
    By thebaul in forum Access
    Replies: 1
    Last Post: 08-01-2012, 05:51 AM
  4. Query to convert String to Date??
    By taimysho0 in forum Programming
    Replies: 3
    Last Post: 06-04-2012, 04:48 PM
  5. Convert string to date
    By ~SwAmPdOnKeY~ in forum Queries
    Replies: 4
    Last Post: 09-11-2008, 07:19 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums