473,385 Members | 1,798 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

SQL Statement - Selecting between dates using dateadd() and dateserial()

Hello everyone,

I was recently tasked to convert some of the functions employees use Excel for into Access. One of these functions is seeing which invoices are due to be paid this month. Surely this is simple I thought, and so it would be - if I had a field with a duedate in it. However I am using the sum of two fields as my date and thus, I feel, I have a problem. It's the end of the day and I'm knackered, can anyone spot my Query error?

The below code is one of the many different ways I've tried to skin this cat...
(There are plenty of records with both [TermsDays] AND [TermsDate] NULL.)
In Query; Datatype mismatch in this case. I assume with the DateAdd?
Expand|Select|Wrap|Line Numbers
  1. SELECT tblPurchaseOrderDetail.*, tblPurchaseOrderDetail.TermsDays, tblPurchaseOrderDetail.TermsDate
  2. FROM tblPurchaseOrdersHeader INNER JOIN tblPurchaseOrderDetail ON tblPurchaseOrdersHeader.OrderNumber = tblPurchaseOrderDetail.OrderNumber
  3. WHERE (((tblPurchaseOrderDetail.TermsDate) Is Not Null) AND ((DateAdd("d",[TermsDays],[TermsDate]))>=DateSerial(Year(Now()),Month(Now()),1) And (DateAdd("d",[TermsDays],[TermsDate]))<DateSerial(Year(Now()),Month(Now()),1)));
  4.  
Any help would be greatly appreciated.

John
Jul 8 '10 #1

✓ answered by nico5038

I would probably use:

Expand|Select|Wrap|Line Numbers
  1. SELECT tblPurchaseOrderDetail.*, tblPurchaseOrderDetail.TermsDays, tblPurchaseOrderDetail.TermsDate
  2. FROM tblPurchaseOrdersHeader INNER JOIN tblPurchaseOrderDetail ON tblPurchaseOrdersHeader.OrderNumber = tblPurchaseOrderDetail.OrderNumber
  3. WHERE tblPurchaseOrderDetail.TermsDate Is Not Null 
  4. AND tblPurchaseOrderDetail.TermsDays Is Not Null 
  5. AND [TermsDate] + [TermsDays] between DateSerial(Year(Date()),Month(Date()),1) And DateSerial(Year(Date()),Month(Date())+1,0)));
  6.  
Adding days can be done using simply a "+", being faster as the DateAdd function. Using the 0 in the Dateserial will give the last day of a month.
Using "Between" is faster as it's just one comparison instead of two when using >= and <=.

Guess you would also need a report showing records without a Date and/or term, as they will be skipped...

Nic;o)

3 4625
nico5038
3,080 Expert 2GB
I would probably use:

Expand|Select|Wrap|Line Numbers
  1. SELECT tblPurchaseOrderDetail.*, tblPurchaseOrderDetail.TermsDays, tblPurchaseOrderDetail.TermsDate
  2. FROM tblPurchaseOrdersHeader INNER JOIN tblPurchaseOrderDetail ON tblPurchaseOrdersHeader.OrderNumber = tblPurchaseOrderDetail.OrderNumber
  3. WHERE tblPurchaseOrderDetail.TermsDate Is Not Null 
  4. AND tblPurchaseOrderDetail.TermsDays Is Not Null 
  5. AND [TermsDate] + [TermsDays] between DateSerial(Year(Date()),Month(Date()),1) And DateSerial(Year(Date()),Month(Date())+1,0)));
  6.  
Adding days can be done using simply a "+", being faster as the DateAdd function. Using the 0 in the Dateserial will give the last day of a month.
Using "Between" is faster as it's just one comparison instead of two when using >= and <=.

Guess you would also need a report showing records without a Date and/or term, as they will be skipped...

Nic;o)
Jul 8 '10 #2
Thats bang on, thanks very much Nico! Don't know how I got stuck on that, but everythings simple in retrospect right? I'm grateful for the quick concise answer!

John
Jul 8 '10 #3
nico5038
3,080 Expert 2GB
Getting things simple is the hardest part <LOL>

Glad I could help and success with your application !

Nic;o)
Jul 9 '10 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: bissatch | last post by:
Hi, How do I compare two date stored within a database with datatype 'date'? The dates are stored within a PostgreSQL database table in the format "Y-m-d". Cheers
1
by: Fons Dijkstra | last post by:
Hello, I'm using the mx.ODBC.Windows package in order to read/write a MSAccess database. Everything works fine apart from the DATE format handling. I'm using the default "datetimeformat" (i.e....
1
by: PMBragg | last post by:
ORINGINAL Post >Thank you in advance. I'm trying to pull all inventory items from December >of the previous year back to 4 years for my accountant. I know this can be >done, but I'm drawing a...
2
by: Paul# | last post by:
how do i query for a record between two dates using Access 2000? right now Im doing this: SELECT * FROM tblDateTotal WHERE .<>0 And . Between 1/1/1901 And 10/10/2050;
6
by: Bill R via AccessMonster.com | last post by:
I have a query: SELECT tblCalendar.CalendarDay AS LastSunday FROM tblCalendar WHERE (((tblCalendar.CalendarDay)>=(Now()-7) And (tblCalendar.CalendarDay)...
4
by: Good Man | last post by:
Hi there I have a list of jobs scheduled in a MySQL table, with start dates and end dates, like so: SchedID | JobID | StartDate | EndDate |...
1
by: ode | last post by:
I am an infrequent user and my version is Access 2000. I have a list of 122k lines which consits of 3 fields/data points;a product code, a date, and a price code. I want to run a query to select...
3
by: ringer | last post by:
On a form I have a combo where a month is entered. It shows the month names in column one, but it is bound to column two where each month's corresponding number is, ie Jan=1, Feb=2, etc. So...
3
by: samvb | last post by:
Hi, I have this query: SELECT finishtime, bonas, dateadd('s',bonas,finishtime) AS fmore FROM scores; It works great but I need to order the result by fmore. It generates errors. Is...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.