Start date End date Range from SSRS Parameters against SQL Server 2005

Im tryin to calculate a date range given two calendar input boxes for start and end date;
these paramateres are defined as datetime; as well as datetime in the stored procedure;
However, in the table all of the fields are defined as varchar(50): in the format YYYYMMDD
What is the easiest way I can code this date range for my where clause in which I pass these two mandatory fields to my stored procedure?

I have tried numerous cast and conversion functions but I get an out of bound error when taking in both parameters.  Which is the cleanest way to code this for my where clause?

@startdate varchar(50)
@enddate varchar(50)

Open in new window

Who is Participating?
Chris LuttrellConnect With a Mentor Senior Database ArchitectCommented:
If you cannot do SELECT convert(datetime,yourDBVarcharField) where you think all the values are in the format 'YYYYMMDD' then you have some invalid dates in there which is what is causing the out of bound error.  It usually is something like '00000000' or '19000000' that is used as a place holder insted of Nulls.  You will have to exclude those dates or do some kind of CASE conversion on them.
Use Datetime for the stored proc parameters, then convert them to varchar inside the proc.  Then you can use the SSRS date field with calendar control.

for example:

sp_testreport @StartdateD datetime,@EndDateD datetime as

declare @StarrtDate varchar(10), @EndDate varchar(10)

select @StartDate=convert(varchar(4),year(@StartDateD))+

select @EndDate=convert(varchar(4),year(@EndDateD ))+
                               right('0'+convert(varchar(2),month(@EndDateD )),2)+
                               right('0'+convert(varchar(2),day(@EndDateD )),2)

select * from Table where Datefield between @StartDate and @EndDate

jtrapat1Author Commented:

You were absolutely right-
I had blanks in the fields for my dates -
And default values of 19000101.

I thought I had tested for NULLs, spaces, etc....

Thanks Again.
All Courses

From novice to tech pro — start learning today.