Hi guy when i run this Stored procedure it give me message error

conversion faild when converting date time from charachter sting

stored procedure as following

Create proc searchData

    @StartDate datetime, 
    @EndDate datetime

Declare @SQLQuery as nvarchar(2000)
SET @SQLQuery ='SELECT * from Employee Where (1=1)' 
  If (@StartDate is not NULL) AND (@EndDate is not NULL)
         Set @SQLQuery = @SQLQuery + ' And (JoinDate 
         BETWEEN '+ @StartDate +' AND '+@EndDate+')'
Exec (@SQLQuery)

JoinDate found in table Employee as datetime

but when i make stored procedure as following

it work in formate dd/mm/yyyy and this is what i need

ALTER  proc [dbo].[searchData]

@StartDate datetime 

@EndDate  datetime, 


select * from dbo.Employee e where JoinDate between @StartDate and @EndDate

Now what is the proplem in first stored procedure
Please help me if possible

3 Years
Discussion Span
Last Post by ChrisHunter

If you're writing the stored proc within the C# code us double quotation marks instead of single when you're writing the SQL string.

Single quotation marks are used define chars in SQL so it might be trying to pass in the charactors @StartDate ranter than a datetime.

Or have you posted this in the wrong forum?

Edited by ChrisHunter

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.