Solved

SQL - WHERE date range

Posted on 2004-10-12
5
667 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
  • 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 115 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

When you hear the word proxy, you may become apprehensive. This article will help you to understand Proxy and when it is useful. Let's talk Proxy for SQL Server. (Not in terms of Internet access.) Typically, you'll run into this type of problem w…
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 extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

789 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