Solved

SQL - WHERE date range

Posted on 2004-10-12
5
670 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 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SQL 2012 AOG and SQL2014 AOG 76 61
SSIS GUID Variable 2 37
Why is this SQL bringing back extra rows? (parsing XML data) 4 39
Need to find substring in SQL 3 13
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
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
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.

733 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