Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Between in SQL Server Stored Procedure Problem

Posted on 2003-12-09
5
Medium Priority
?
691 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 70

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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
An alternative to the "For XML" way of pivoting and concatenating result sets into strings, and an easy introduction to "common table expressions" (CTEs). Being someone who is always looking for alternatives to "work your data", I came across this …
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.

604 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