Solved

query to return a date range.

Posted on 2011-03-01
4
250 Views
Last Modified: 2012-05-11
I would like to use the query below to return a date range but its key is the LOUReturn field which determins which records in the date field. i'd also like to drop the hour/min/sec from the query. the code below parses ok but doesn't return any thing, but is i run it without the between date part all the data comes back. also I'll be building this query into SSRS report for weekly run.

use PCTSRS
select d.CM_ITEM_ID, d.CM_ITEM_NAME, d.CLIN, d.SEARCH_APPROVING_OFFICIAL as 'SAO', d.SEARCH_PROFESSIONAL as 'SP',
         r.FixedByPTO, r.DateReturned, r.LOUReturn, r.ErrorText
      from CLG_RETURNS r
            inner join CLG_DOCUMENTS d on r.ItemID = d.CM_ITEM_ID
      where r.LOUReturn = (1)and r.DateReturned between '2011-02-16 00:00:00.000' and '2010-03-10 00:00:00.000'
      
      order by r.DateReturned desc
0
Comment
Question by:richa1960
  • 2
4 Comments
 
LVL 22

Accepted Solution

by:
Thomasian earned 500 total points
ID: 35008455
>> r.DateReturned between '2011-02-16 00:00:00.000' and '2010-03-10 00:00:00.000'

That is because your "from" date is greater than you "to" date

Maybe you meant:

r.DateReturned between '2011-02-16 00:00:00.000' and '2011-03-10 00:00:00.000'
0
 
LVL 25

Expert Comment

by:reb73
ID: 35008471
Swap the datereturned values around, the lower value first and the higher value after..
0
 
LVL 10

Expert Comment

by:itcouple
ID: 35008635
Hi

Also make sure your date field doesn't contain time if it does than use DateDiff function for compare dates whcih usually is recommended for any date comparisons criterias.

Regards
Emil
0
 
LVL 25

Expert Comment

by:reb73
ID: 35008704
You could use a template like below (or even more preferable convert it to a parameterized stored procedure) -
use PCTSRS

declare @startdate datetime, @enddate datetime, @LOUReturn int

select	 @startdate = '2011-02-16'	-- Populate startdate her
	,@enddate = '2011-03-01'	-- populate enddate here
	,@LOUReturn = 1			-- Populate LouReturn value here

set @enddate = dateadd(second, -1, dateadd(day,1,@enddate))

select d.CM_ITEM_ID, d.CM_ITEM_NAME, d.CLIN, d.SEARCH_APPROVING_OFFICIAL as 'SAO', d.SEARCH_PROFESSIONAL as 'SP',
         r.FixedByPTO, r.DateReturned, r.LOUReturn, r.ErrorText 
from CLG_RETURNS r
            inner join CLG_DOCUMENTS d on r.ItemID = d.CM_ITEM_ID
where r.LOUReturn = @LOUReturn and r.DateReturned between @startdate and @enddate

Open in new window

0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

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.
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

705 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now