Solved

Between in SQL Server Stored Procedure Problem

Posted on 2003-12-09
5
668 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 33 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 33 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 34 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

SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

Question has a verified solution.

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

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
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…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
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.

734 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