SQL for If, Then in a Query?

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I need to create a calculated field in a query that looks at the 'Complete
Date' and returns the quarter the deal completed. Example, if the complete
date is 2/15/05, then it completed in Q1 and the query should return 1 in
that record. Similarly if the date is 6/3/05, then 2 (the year is irrelevant
for this field). The powers that be can't look at the date and calculate the
quarter in their head so I have to do it for them in this report.

I was thinking an SQL subquery would work but I haven't a clue how to write
that.

I can code the data input form and populate the table driving the above
query but, I prefer to have the calculation in the Query because it will
catch any data changes/updates in the table (which are unlikely but may
happen). Am I barking up the wrong tree with the SQL? And if not, anyone
know how to write that?
 
Use the DatePart function
DatePart{"q",[Complete Date])

Or use the format function
Format([Complete Date],"q")

Or use the format property of a control bound to the Complete Date
 
You should be able to used the following as a calculated field in your query:

Quarter: DatePart("q",[Complete Date])
 
And here I was banging my head against the wall. Works perfectly. Thanks

JJEWELL said:
You should be able to used the following as a calculated field in your query:

Quarter: DatePart("q",[Complete Date])


Tedd N said:
I need to create a calculated field in a query that looks at the 'Complete
Date' and returns the quarter the deal completed. Example, if the complete
date is 2/15/05, then it completed in Q1 and the query should return 1 in
that record. Similarly if the date is 6/3/05, then 2 (the year is irrelevant
for this field). The powers that be can't look at the date and calculate the
quarter in their head so I have to do it for them in this report.

I was thinking an SQL subquery would work but I haven't a clue how to write
that.

I can code the data input form and populate the table driving the above
query but, I prefer to have the calculation in the Query because it will
catch any data changes/updates in the table (which are unlikely but may
happen). Am I barking up the wrong tree with the SQL? And if not, anyone
know how to write that?
 
This is much easier than I thought, thanks

John Spencer said:
Use the DatePart function
DatePart{"q",[Complete Date])

Or use the format function
Format([Complete Date],"q")

Or use the format property of a control bound to the Complete Date
Tedd N said:
I need to create a calculated field in a query that looks at the 'Complete
Date' and returns the quarter the deal completed. Example, if the
complete
date is 2/15/05, then it completed in Q1 and the query should return 1 in
that record. Similarly if the date is 6/3/05, then 2 (the year is
irrelevant
for this field). The powers that be can't look at the date and calculate
the
quarter in their head so I have to do it for them in this report.

I was thinking an SQL subquery would work but I haven't a clue how to
write
that.

I can code the data input form and populate the table driving the above
query but, I prefer to have the calculation in the Query because it will
catch any data changes/updates in the table (which are unlikely but may
happen). Am I barking up the wrong tree with the SQL? And if not, anyone
know how to write that?
 
Tedd said:
I need to create a calculated field in a query that looks at the 'Complete
Date' and returns the quarter the deal completed. Example, if the complete
date is 2/15/05, then it completed in Q1 and the query should return 1 in
that record. Similarly if the date is 6/3/05, then 2 (the year is irrelevant
for this field). The powers that be can't look at the date and calculate the
quarter in their head so I have to do it for them in this report.

I was thinking an SQL subquery would work but I haven't a clue how to write
that.

I can code the data input form and populate the table driving the above
query but, I prefer to have the calculation in the Query because it will
catch any data changes/updates in the table (which are unlikely but may
happen). Am I barking up the wrong tree with the SQL? And if not, anyone
know how to write that?


The calculated field can be just:
DatePart("Q", [Complete Date])
 

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

Similar Threads

The query cannot be completed. 1
grouping quarters 3
Combine Duplicates in Query 0
Query for a report 1
Count Query Over Multiple Fields 1
Criteria in Query 1
Problem with query 2
query sql needed 1

Back
Top