Results 1 to 7 of 7
  1. #1
    Osman is offline Novice
    Windows XP Access 2003
    Join Date
    Oct 2010
    Posts
    5

    Question How to change year digit in update query?

    Hello,

    I have date field in an Access table.
    I want to change only the year of that field using an update query.

    Example:
    10.10.2008 my date value

    I want to pass the current year 2010 to that field and
    I want it to be 10.10.2010.



    The month and day digits will not change. But the year will change every year according to the current year like below :
    10.10.2010
    10.10.2011
    10.10.2012

    Thank you

  2. #2
    jzwp11 is offline VIP
    Windows 7 Access 2007
    Join Date
    Jun 2010
    Location
    Dayton, OH
    Posts
    2,901
    Welcome to the forum!

    If the field is a date/time datatype, they you can use the dateadd() function with the year option. If all of the dates are 2008 then to update them to 2010, the dateadd() function would look like this:

    dateadd("yyyy",2, myDateField)

    The query might look something like this (you will have to substitute your own table and field names)

    UPDATE Table1 SET Table1.myDateField = DateAdd("yyyy",2,mydatefield);

  3. #3
    Osman is offline Novice
    Windows XP Access 2003
    Join Date
    Oct 2010
    Posts
    5
    Thank you for your welcome message.

    Quote Originally Posted by jzwp11 View Post
    dateadd("yyyy",2, myDateField)
    I know dateadd function. But the years are not only 2008. They vary.
    That's why, I can't use fixed numbers.

  4. #4
    jzwp11 is offline VIP
    Windows 7 Access 2007
    Join Date
    Jun 2010
    Location
    Dayton, OH
    Posts
    2,901
    You can use an expression instead of the 2 within the dateadd() function that yields the correct number of years to add

    dateadd("yyyy", 2010-year(mydatefield), myDateField)


    Out of curiosity, you said this:
    But the year will change every year according to the current year like below :
    Can you explain why you need to do this? What do the dates represent?

  5. #5
    jzwp11 is offline VIP
    Windows 7 Access 2007
    Join Date
    Jun 2010
    Location
    Dayton, OH
    Posts
    2,901
    To make the expression even more versatile, you can modify the expression for the current year:

    dateadd("yyyy", year(date())-year(mydatefield), myDateField)

  6. #6
    Osman is offline Novice
    Windows XP Access 2003
    Join Date
    Oct 2010
    Posts
    5

    Thumbs up

    dateadd("yyyy", year(date())-year(mydatefield), myDateField)

    This solved my problem. Thank you very much indeed.

  7. #7
    jzwp11 is offline VIP
    Windows 7 Access 2007
    Join Date
    Jun 2010
    Location
    Dayton, OH
    Posts
    2,901
    You are welcome. Good luck with your project.

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

Similar Threads

  1. change textbox value after combo update
    By arctushar@yahoo.com in forum Forms
    Replies: 5
    Last Post: 09-06-2010, 07:17 PM
  2. display single digit
    By appiades in forum Access
    Replies: 2
    Last Post: 07-05-2010, 05:36 PM
  3. Adding a 0 to single digit imports
    By TheWolfster in forum Access
    Replies: 6
    Last Post: 02-16-2010, 05:19 PM
  4. Query to extract record of particular year
    By pkg206 in forum Access
    Replies: 2
    Last Post: 11-11-2009, 10:01 PM
  5. Replies: 1
    Last Post: 09-01-2006, 03:49 PM

Tags for this Thread

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