Results 1 to 11 of 11
  1. #1
    mlevel is offline Novice
    Windows 11 Access 2016
    Join Date
    Mar 2023
    Posts
    3

    Populate a table column with rearranged content from another column


    This SHOULD be doable with some kind of query but I cannot figure how or where to enter it.

    I created a table many years ago with dates as text in mm/dd/yyyy format. I want dates in yyyy-mm-dd format. I added a newdate column that's empty.

    Should be something like:
    replace all newdate with substr(olddate, 5,4) + "-" + substr(olddate,1,2) + "-" + substr(olddate, 3,2)

    I could've done this in a minute in FoxPro (I am old). I've tried to figure it out in Access for like 15 years now!

    It's just a hobby database (authors and quotations). Where would I enter this command in Access and what's the correct syntax?

    Eternal gratitude for an answer that works...

    Steve M.

  2. #2
    Micron is online now Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,801
    Since your dates are strings, one way is in query design:
    Code:
    NewDate:DatePart("yyyy",cdate(olddate)) & "-" & DatePart("m",cdate(olddate)) & "-" & DatePart("d",cdate(olddate))
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  3. #3
    Micron is online now Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,801
    But simpler, perhaps
    format(datevalue(olddate),"yyyy-mm-dd")
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  4. #4
    orange's Avatar
    orange is offline Moderator
    Windows 10 Office 365
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,726
    ?? Why would you define a Date as a string/text ??

    CDate(your textual Date) will return a Date.
    But formating a date results in a string

    Sample:

    Code:
    Sub testcDate()
        Dim X As String: X = "10/23/2016"
        Dim D As Date
        Dim Y As String
        D = CDate(X)
        Y = Format(D, "yyyy-mm-dd") 'this reult will still be a string
        Debug.Print Y
    End Sub
    Result: 2016-10-23

  5. #5
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,940
    Quote Originally Posted by Micron View Post
    But simpler, perhaps
    format(datevalue(olddate),"yyyy-mm-dd")
    Still a string though?
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  6. #6
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,940
    Quote Originally Posted by mlevel View Post
    This SHOULD be doable with some kind of query but I cannot figure how or where to enter it.

    I created a table many years ago with dates as text in mm/dd/yyyy format. I want dates in yyyy-mm-dd format. I added a newdate column that's empty.

    Should be something like:
    replace all newdate with substr(olddate, 5,4) + "-" + substr(olddate,1,2) + "-" + substr(olddate, 3,2)

    I could've done this in a minute in FoxPro (I am old). I've tried to figure it out in Access for like 15 years now!

    It's just a hobby database (authors and quotations). Where would I enter this command in Access and what's the correct syntax?

    Eternal gratitude for an answer that works...

    Steve M.
    Just make your dates actual dates, and then format them how you want, but leave them as actual dates.
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  7. #7
    Micron is online now Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,801
    OK, it's obvious that the original values are text, not that I think it's good. I chose to ignore that given that the values could have come from another source, such as a csv file. So yes, still text but it answers the question. If OP wants to know how to convert the strings to dates, then I imagine he/she will ask.
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  8. #8
    mlevel is offline Novice
    Windows 11 Access 2016
    Join Date
    Mar 2023
    Posts
    3
    Quote Originally Posted by Micron View Post
    Since your dates are strings, one way is in query design:
    Code:
    NewDate:DatePart("yyyy",cdate(olddate)) & "-" & DatePart("m",cdate(olddate)) & "-" & DatePart("d",cdate(olddate))
    Thanks very much for the reply (the fields are text because it's a very old database, but also because we sometimes don't know exactly when a person was born or died, so I have incomplete dates like 07/12/192? that seem to be problematic in strict date format).

    Apologies, but I really know nothing of tinkering in Access. So I would go to Query Design, select my People table that contains these two fields, and then... where do I go to enter this code?

  9. #9
    Micron is online now Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,801
    At the very top of the query design grid. Enter in a new column. A date with a ? is kinda dumb. Date data type and no dates for the unknowns would have been better.
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  10. #10
    mlevel is offline Novice
    Windows 11 Access 2016
    Join Date
    Mar 2023
    Posts
    3
    Thanks again.

    This works, kinda, but is also "kinda dumb" for my purposes. Also, it doesn't add the new column to the table?

    I really want to keep the dates in string form because the table contains data about people. I need *approximate* dates sometimes where nobody knows the *exact* date a person was born, but I still want to be *close* and not throw away the part of the date that I *do* know.

  11. #11
    Micron is online now Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,801
    Then change the select query to an update query if you already have added a table field. Or just overwrite the existing values in the original field? I'd copy the table first in case that goes bad.
    Last edited by Micron; 03-26-2023 at 09:37 AM. Reason: removed comment
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 4
    Last Post: 09-18-2019, 05:55 AM
  2. Replies: 5
    Last Post: 03-07-2019, 09:00 AM
  3. Replies: 3
    Last Post: 07-06-2016, 09:48 AM
  4. Replies: 15
    Last Post: 01-12-2011, 05:13 PM
  5. Replies: 3
    Last Post: 05-19-2010, 10:08 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