Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

How do I return records from a date range with SQL syntax?

Posted on 2007-12-04
6
Medium Priority
?
549 Views
Last Modified: 2010-05-18
I have an SQL table of records from 2006-2007 and I want to select a range of records based upon start and stop dates entered into text boxes.

I'm using Visual Web Developer; SQL Express; Express Management Studio; VB backcoding

Here's my statement:

SELECT [ID], [BILLNUMBER], [CHARGEDATE], [LNAME], [FNAME] FROM [TABLETracking] WHERE (([CHARGEDATE] IS NOT NULL) AND ([CHARGEDATE] >= @CHARGEDATE) AND ([CHARGEDATE] <= @CHARGEDATE2) AND ([7CAT] = @7CAT))


So, the @CHARGEDATE and @CHARGEDATE2 values are coming from textbox dates that are entered. @7CAT is a value from a user-select dropdown list.

However, a start and stop date such as 01/01/2007 to 12/31/2007 will also return records from the year 2006 (arrrghhhh!...frustrating). How can I make this work? I know there is a BETWEEN statement that can be used, but I am unable to get anything to work..... maybe my parentheses are not structured correctly. Thanks for any assistance.
0
Comment
Question by:jazjef
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 3

Expert Comment

by:cforant
ID: 20406280
SELECT column_name FROM table_name
WHERE column_name
BETWEEN value1 AND value2
0
 
LVL 23

Accepted Solution

by:
Ashish Patel earned 2000 total points
ID: 20406447
Try this.
SELECT [ID], [BILLNUMBER], [CHARGEDATE], [LNAME], [FNAME] FROM [TABLETracking] WHERE [CHARGEDATE] IS NOT NULL AND [CHARGEDATE] BETWEEN @CHARGEDATE AND @CHARGEDATE2 AND [7CAT] = @7CAT

0
 
LVL 4

Author Comment

by:jazjef
ID: 20406575
asvforce:

thanks for the reply... your statement works fine, but I still get records from 2006 when I use it.
0
Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

 
LVL 23

Expert Comment

by:Ashish Patel
ID: 20406617
What is the column type of CHARGEDATE column?
0
 
LVL 4

Author Comment

by:jazjef
ID: 20406853
ASVFORCE:

column type was char(10)

this was causing the problem.... when I converted to datetime your solution works.... and so does my own original solution prior to asking the question.... thanks.

I have another problem though.... I will post a question on it momentarily. It's related to this datetime thing...
0
 
LVL 23

Expert Comment

by:Ashish Patel
ID: 20406865
Good catch!!! Glad to hear that you have solved this now.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I'm trying, I really am. But I've seen so many wrong approaches involving date(time) boundaries I despair about my inability to explain it. I've seen quite a few recently that define a non-leap year as 364 days, or 366 days and the list goes on. …
In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…

688 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