Click here to Skip to main content
15,881,424 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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

SQL
Create proc searchData

    @StartDate datetime, 
    @EndDate datetime

as
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

SQL
ALTER  proc [dbo].[searchData]

@StartDate datetime 

@EndDate  datetime, 

as

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
Posted
Updated 1-Sep-14 5:01am
v3
Comments
ChauhanAjay 1-Sep-14 3:15am    
Try this
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)
George Jonsson 1-Sep-14 11:03am    
How do you call the stored procedure?
What is your locale settings?

Use sp_executesql[^] instead of EXEC:
SQL
Declare @SQLQuery as nvarchar(2000);

SET @SQLQuery = N'SELECT * from Employee Where (1=1)';

If (@StartDate is not NULL) AND (@EndDate is not NULL)
    SET @SQLQuery = @SQLQuery + N' And (JoinDate Between @StartDate And @EndDate)';

EXEC sp_executesql @SQLQuery,
    N'@StartDate datetime, @EndDate datetime',
    @StartDate, @EndDate;
 
Share this answer
 
Make following changes in your procedure it will definitely work for you.
SQL
CREATE PROCEDURE searchData
@StartDate DATETIME,
@EndDate DATETIME
AS
BEGIN
	--@DemoDate is just place holder if JoinDate is null in table
	DECLARE @DemoDate DATETIME
	SET @DemoDate = GETDATE()

	SELECT * FROM Employee
	WHERE COALESCE(JoinDate, @DemoDate) 
	BETWEEN COALESCE(@StartDate, JoinDate, @DemoDate)
	AND COALESCE(@EndDate, JoinDate, @DemoDate)
END

Whether parameters @StartDate, @EndDate having null values or date values your procedure will work fine. If you are dam sure that JoinDate in table is always not null then following changes are sufficient.
SQL
CREATE PROCEDURE searchData
@StartDate DATETIME,
@EndDate DATETIME
AS
BEGIN
	SELECT * FROM Employee
	WHERE JoinDate
	BETWEEN COALESCE(@StartDate, JoinDate)
	AND COALESCE(@EndDate, JoinDate)
END

Any suggestions are acceptable.
 
Share this answer
 
Change your first procedure in the following format

SQL
Create proc searchData

    @StartDate datetime,
    @EndDate datetime

as
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 '''+ convert(varchar, @StartDate, 103) +''' AND '''+convert(varchar, @EndDate, 103)+''')'
Exec (@SQLQuery)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900