?
Solved

SQL - WHERE date range

Posted on 2004-10-12
5
Medium Priority
?
679 Views
Last Modified: 2010-08-05
Hello,

In a SQL Server database I have a column called datereceived.  In datereceived an example of a value is '20040513'.

How am I able to run a SQL SELECT statement and have the WHERE select a date?  So, for example, lets say I want to pull all data where datereceived is between Sept. 1 and Sept . 30.

Thanks in advance.
0
Comment
Question by:rudyflyer
[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
5 Comments
 
LVL 69

Expert Comment

by:Scott Pletcher
ID: 12292607
SELECT ...
FROM ...
WHERE dateReceived BETWEEN '20040901' AND '20040930'



Is dateReceived a [SMALL]DATETIME type or a plain numeric type, like INT?  If [SMALL]DATETIME, you need to add time to the last value:

WHERE dateReceived BETWEEN '20040901' AND '20040930 23:59'  -- smalldatetime
WHERE dateReceived BETWEEN '20040901' AND '20040930 23:59:59.997'  -- datetime

--NOTE: .997 is correct, *not* .999, which will round up to the next day.



0
 
LVL 6

Accepted Solution

by:
acampoma earned 460 total points
ID: 12292618
select * from TableName where datereceived  between '20040901' and '20040930'
0
 

Author Comment

by:rudyflyer
ID: 12292665
datereceived is numeric.

Would it be possible to have the the WHERE clause state:

...WHERE dateReceived BETWEEN '09/01/04' AND '09/30/04'.  

Thanks.
0
 
LVL 69

Expert Comment

by:Scott Pletcher
ID: 12297892
Only if you convert dateReceived to a [small]datetime first.  The huge disadvantage to that is that now an index on dateReceived will not be used, even if one exists:


select *
from TableName
where CAST(datereceived AS SMALLDATETIME)  between '09/01/04' and '09/30/04'
0
 
LVL 69

Expert Comment

by:Scott Pletcher
ID: 12298109
Sorry, just realized you accept the [same] answer from someone else.  I will get out of your way and allow him/her to respond.
0

Featured Post

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

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

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.
Suggested Courses

800 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