Solved

Between in SQL Server Stored Procedure Problem

Posted on 2003-12-09
5
628 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
5 Comments
 
LVL 26

Assisted Solution

by:Hilaire
Hilaire earned 33 total points
Comment Utility
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:dishanf
dishanf earned 33 total points
Comment Utility
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:dishanf
Comment Utility
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:
ScottPletcher earned 34 total points
Comment Utility
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
Comment Utility
Thanks for the suggestions.  Points for all since all methods work.

0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

In this article—a derivative of my DaytaBase.org blog post (http://daytabase.org/2011/06/18/what-week-is-it/)—I will explore a few different perspectives on which week today's date falls within using Microsoft SQL Server. First, to frame this stu…
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…
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

771 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

10 Experts available now in Live!

Get 1:1 Help Now