?
Solved

Between in SQL Server Stored Procedure Problem

Posted on 2003-12-09
5
Medium Priority
?
681 Views
Last Modified: 2008-02-26
Here is my stored procedure:

CREATE PROCEDURE summary
      @startdate varchar(20),
      @enddate varchar(20)
AS
select * from  SanderGroup
where Entrydate between CONVERT(varchar, CONVERT(smalldatetime, @startdate)) and  CONVERT(varchar, CONVERT(smalldatetime, @enddate))
GO

I was under the understanding that between is inclusive, so if I used 11/03/03 as my startdate and 11/05/03 as my end date I should get records with 11/03/03, 11/04/03, AND 11/05/03

But I don't I only get 3, and 4.  Am I missing something?

TIA
0
Comment
Question by:Tigger996
[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
5 Comments
 
LVL 26

Assisted Solution

by:Hilaire
Hilaire earned 132 total points
ID: 9910472
Yes
you could use this code instead

CREATE PROCEDURE summary
     @startdate varchar(20),
     @enddate varchar(20)
AS
select * from  SanderGroup
where Entrydate >= CONVERT(varchar, CONVERT(smalldatetime, @startdate)) and  EntryDate < dateadd(d, 1, CONVERT(varchar, CONVERT(smalldatetime, @enddate)))

Dates whithout time part are translated to 0:00 AM

between 11/03/03 0:00 PM and 11/05/03 0:00 PM
will fail to return events that occured on 11/05/03 after 0:00 PM

Hilaire

0
 
LVL 8

Assisted Solution

by:Dishan Fernando
Dishan Fernando earned 132 total points
ID: 9910543
try this..
CREATE PROCEDURE summary
     @startdate varchar(20),
     @enddate varchar(20)
AS
select * from  Agent
where CreationDate between CONVERT(varchar(8), @startdate,101) and  CONVERT(varchar(8), @enddate,101)
GO
0
 
LVL 8

Expert Comment

by:Dishan Fernando
ID: 9910549
table names must be change like this..

select * from  SanderGroup
where Entrydate between CONVERT(varchar(8), @startdate,101) and  CONVERT(varchar(8), @enddate,101)
0
 
LVL 69

Accepted Solution

by:
Scott Pletcher earned 136 total points
ID: 9912550
You need the starting time to be midnight and the ending time to 23:59 (for SMALLDATETIME; 23:59:59.997 for DATETIME).  For example:

select * from  SanderGroup
where Entrydate between CAST(CONVERT(varchar(10), @startdate, 120) AS SMALLDATETIME) and CAST(CONVERT(varchar(10), @enddate, 120) + ' 23:59' AS SMALLDATETIME)
GO


If is safer to use format 120, which is always recognized correctly regardless of server settings, and it allows a time to be specified, which 101 does not.   The VARCHAR(10) on the CONVERT will truncate the time portion, leaving only the date (yyyy-mm-dd).
0
 

Author Comment

by:Tigger996
ID: 9923454
Thanks for the suggestions.  Points for all since all methods work.

0

Featured Post

Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

Question has a verified solution.

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

The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Suggested Courses

777 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