Home > OS >  can you add an extra parameter to DateAdd function?
can you add an extra parameter to DateAdd function?

Time:06-12

i'm using dateadd to sum 2 fields in my access query, but i want to always add "1" year to the result of the dateadd.

Example:

Dateadd('yyyy', [field1],[field2]) 1

it's not working. It's only giving me the sum of Dateadd('yyyy', [field1],[field2])

Field1 = date
Field2 = number

example:

field1 = 2020-01-01

field2 = 2

i want 2020-01-01 2 1(I always want to add 1 year at the end)

I should get 2023-01-01

CodePudding user response:

I think what you want is:

DATEADD(‘yyyy’, ([field2] 1), [field1])

CodePudding user response:

If its giving you that much difficulty, call it twice via

Dateadd('yyyy', 1, Dateadd('yyyy', [field1],[field2]))

So the inner is processed first giving you whatever date years to add. From that, the OUTER DateAdd will just add 1 year to the inner result.

But I suspect it is because your context of field is incorrect. The second parameter is how many years, days, months, whatever you want to advance. The THIRD is the date. So by you having a bad naming sample of field1, field2 instead of MyDateField, MyYearsToAddField, it would make more sense now and the future. So it SHOULD PROBABLY work as

Dateadd('yyyy', 1   MyYearsToAddField, MyDateField )
  • Related