Solved

query to return a date range.

Posted on 2011-03-01
4
253 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

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SQL Server Deadlocks 12 50
T-SQL: Subtracting Amounts from "Among Rows" 3 43
Update data using formula 22 21
TOOLS - convert T-SQL TO PL/SQL 3 0
Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
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.
Via a live example, show how to shrink a transaction log file down to a reasonable size.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

910 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

16 Experts available now in Live!

Get 1:1 Help Now