0

It is my first question at stack overflow. I want to ask if there is any way I could make a field in an Access query which will be based on two other fields? Eg. in the following case, I want to make a field ReqDate in the query which will return DueDate + 1 year if products > 90, and DueDate + 6 months if products < 90.

Example

Gustav
  • 53,498
  • 7
  • 29
  • 55

2 Answers2

1

As this is Access, the query could look like:

Select 
    *, 
    DueDate, 
    products, 
    IIf(products > 90, DateAdd("yyyy", 1, DueDate), DateAdd("m", 6, DueDate)) As ReqDate 
From 
    YourTable;

or, if you like, a bit tighter:

Select 
    *, 
    DueDate, 
    products, 
    DateAdd("m", 6 * (Abs(products > 90) + 1), DueDate) As ReqDate 
From 
    YourTable;
Gustav
  • 53,498
  • 7
  • 29
  • 55
0

To add date you can use the DATEADD function of sql server.

select ID, DueDate, products , 
(CASE 
    WHEN products > 90
        THEN DATEADD(yy, 1, DueDate)
        ELSE DATEADD(mm, 6, DueDate)

END) as ReqDate from table;

Links:

http://www.w3schools.com/sql/func_dateadd.asp

How do I perform an IF...THEN in an SQL SELECT?

Community
  • 1
  • 1
P. Jairaj
  • 1,033
  • 1
  • 6
  • 8