Solved

sql query syntax

Posted on 2006-11-15
4
261 Views
Last Modified: 2012-08-14
Hi,

We have an application that makes a selection from a Access database based on on date that is passed in. For example if we pass in 10/26/2006, it would select all items that match that month in a date field. This has been working great, but for some reason when it comes to December/January, those 2 months pull up the same data.

Here is the statement that has been used to date:

SQL = "SELECT * FROM tbl_cm_schedule WHERE cid=" & Session.Contents("UserID") & " AND (DATEDIFF('m',#" & dDate & "# , cdate) = DATEDIFF('yyyy', #" & dDate & "#, cdate)) ORDER BY cdate"

So if you're viewing data in December, you see both December and January data, and in January you see both Jan. and Dec. data.

Any help is appreciated.
Thanks.
0
Comment
Question by:totallymarc
  • 2
4 Comments
 
LVL 19

Accepted Solution

by:
VoteyDisciple earned 125 total points
ID: 17948905
I'm not sure, to be honest, why that code works for other months.  It's looking for dates where the difference in years is equal to the difference in months?  

In any case, the following should be better..

SQL = "SELECT * FROM tbl_cm_schedule WHERE cid=" & Session.Contents("UserID") & " AND (MONTH(#" & ddate & "#) = MONTH(cdate) AND YEAR(#" & ddate & "#) = YEAR(cdate)) ORDER BY cdate"

0
 
LVL 4

Expert Comment

by:carlmahon
ID: 17948933
This is because for October
DATEDIFF('m',#" & dDate & "# , cdate) = 0
DATEDIFF('yyyy', #" & dDate & "#, cdate)) = 0

As you get to end of year then you have issues. Because a JAN/DEC Dadtediff for the month = 1 and the years also = 1.


VoteyDiciple's SQL will work
0
 
LVL 19

Expert Comment

by:VoteyDisciple
ID: 17948961
I guess what confused me is there ought to be data for, say, February 2004 showing up in the April 2006 results as well.  Unless the data just don't go back that far, in which case this problem would have become more extensive as more years passed.  (-:
0
 

Author Comment

by:totallymarc
ID: 17948969
Thank you kindly, works fantastic now.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Read about why website design really matters in today's demanding market.
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now