Solved

query to return a date range.

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

Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

Question has a verified solution.

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

Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

785 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