Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

SQL - WHERE date range

Posted on 2004-10-12
5
Medium Priority
?
683 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 70

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 70

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 70

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

Veeam and MySQL: How to Perform Backup & Recovery

MySQL and the MariaDB variant are among the most used databases in Linux environments, and many critical applications support their data on them. Watch this recorded webinar to find out how Veeam Backup & Replication allows you to get consistent backups of MySQL databases.

Question has a verified solution.

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

Windocks is an independent port of Docker's open source to Windows.   This article introduces the use of SQL Server in containers, with integrated support of SQL Server database cloning.
One of the most important things in an application is the query performance. This article intends to give you good tips to improve the performance of your queries.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
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.

876 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