how do i write if and else statement in access query

G

Guest

Hi

I have 2 date fields eg date1 and date 2. I would like to create an
expression in access query with if and else statement to count the number of
day diff between date 1 and date 2.
If the expression WTime : (sum(4-([date 1]-[date 2]))) is positive value
– display the positive value, if the sum is equal to a negative value – to
display it as ‘0†value.

Do you have an idea how to write in the query?
 
G

Guest

Try this, using iif with datediff
sum(IIf(datediff("d",[date1],[date2])>0,datediff("d",[date1],[date2]),0))
 
F

fredg

Hi

I have 2 date fields eg date1 and date 2. I would like to create an
expression in access query with if and else statement to count the number of
day diff between date 1 and date 2.
If the expression WTime : (sum(4-([date 1]-[date 2]))) is positive value
¡V display the positive value, if the sum is equal to a negative value ¡V to
display it as ¡¥0¡¨ value.

Do you have an idea how to write in the query?

In a query you would use an IIF statement:

Exp:IIf(sum(4-([date 1]-[date 2]))>0,sum(4-([date 1]-[date 2])),0)

Create a Totals query (by clicking on the Sigma tool button.
Change Group By to Expression for this column.
 
J

John Spencer (MVP)

Use an immediate if statement

WTime: IIF((sum(4-([date 1]-[date 2])))>0,(sum(4-([date 1]-[date 2]))),0)
 
G

Guest

Hi John Spenser

The expression works for me. Thanks.

I have another question which needs your help -
if there is a blank [date 1] in my earlier expression
WTime : (sum(4-([date 1]-[date 2]))) - the result will reflect as a "blank"
entry and this is what I wish to maintain for statistics purpose. How do I
incorporate this criteria in the expression you have shown me.
I appreciate your help. Thank you once again.


John Spencer (MVP) said:
Use an immediate if statement

WTime: IIF((sum(4-([date 1]-[date 2])))>0,(sum(4-([date 1]-[date 2]))),0)

ann said:
Hi

I have 2 date fields eg date1 and date 2. I would like to create an
expression in access query with if and else statement to count the number of
day diff between date 1 and date 2.
If the expression WTime : (sum(4-([date 1]-[date 2]))) is positive value
– display the positive value, if the sum is equal to a negative value – to
display it as ‘0â€* value.

Do you have an idea how to write in the query?
 
G

Guest

Hi fredg,

Thanks. Your expression is right for me. I appreciate your help.

fredg said:
Hi

I have 2 date fields eg date1 and date 2. I would like to create an
expression in access query with if and else statement to count the number of
day diff between date 1 and date 2.
If the expression WTime : (sum(4-([date 1]-[date 2]))) is positive value
– display the positive value, if the sum is equal to a negative value – to
display it as ‘0†value.

Do you have an idea how to write in the query?

In a query you would use an IIF statement:

Exp:IIf(sum(4-([date 1]-[date 2]))>0,sum(4-([date 1]-[date 2])),0)

Create a Totals query (by clicking on the Sigma tool button.
Change Group By to Expression for this column.
 
G

Guest

Hi Ofer,

The expressions from fredg and John Spencer are what I am looking for. Thank
for your help.

Ofer said:
Try this, using iif with datediff
sum(IIf(datediff("d",[date1],[date2])>0,datediff("d",[date1],[date2]),0))

ann chan said:
Hi

I have 2 date fields eg date1 and date 2. I would like to create an
expression in access query with if and else statement to count the number of
day diff between date 1 and date 2.
If the expression WTime : (sum(4-([date 1]-[date 2]))) is positive value
– display the positive value, if the sum is equal to a negative value – to
display it as ‘0†value.

Do you have an idea how to write in the query?
 
J

John Spencer (MVP)

One way.

WTime: IIF([Date 1] Is Null, Null, IIF((sum(4-([date 1]-[date
2])))>0,(sum(4-([date 1]-[date 2]))),0))

ann said:
Hi John Spenser

The expression works for me. Thanks.

I have another question which needs your help -
if there is a blank [date 1] in my earlier expression
WTime : (sum(4-([date 1]-[date 2]))) - the result will reflect as a "blank"
entry and this is what I wish to maintain for statistics purpose. How do I
incorporate this criteria in the expression you have shown me.
I appreciate your help. Thank you once again.

John Spencer (MVP) said:
Use an immediate if statement

WTime: IIF((sum(4-([date 1]-[date 2])))>0,(sum(4-([date 1]-[date 2]))),0)

ann said:
Hi

I have 2 date fields eg date1 and date 2. I would like to create an
expression in access query with if and else statement to count the number of
day diff between date 1 and date 2.
If the expression WTime : (sum(4-([date 1]-[date 2]))) is positive value
– display the positive value, if the sum is equal to a negative value – to
display it as ‘0â€* value.

Do you have an idea how to write in the query?
 
G

Guest

Hi,

Great!, the expression works. Thanks once again.

John Spencer (MVP) said:
One way.

WTime: IIF([Date 1] Is Null, Null, IIF((sum(4-([date 1]-[date
2])))>0,(sum(4-([date 1]-[date 2]))),0))

ann said:
Hi John Spenser

The expression works for me. Thanks.

I have another question which needs your help -
if there is a blank [date 1] in my earlier expression
WTime : (sum(4-([date 1]-[date 2]))) - the result will reflect as a "blank"
entry and this is what I wish to maintain for statistics purpose. How do I
incorporate this criteria in the expression you have shown me.
I appreciate your help. Thank you once again.

John Spencer (MVP) said:
Use an immediate if statement

WTime: IIF((sum(4-([date 1]-[date 2])))>0,(sum(4-([date 1]-[date 2]))),0)

ann chan wrote:

Hi

I have 2 date fields eg date1 and date 2. I would like to create an
expression in access query with if and else statement to count the number of
day diff between date 1 and date 2.
If the expression WTime : (sum(4-([date 1]-[date 2]))) is positive value
– display the positive value, if the sum is equal to a negative value – to
display it as ‘0â€* value.

Do you have an idea how to write in the query?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top