The stored procedure shown below isn't working as expected when I am passing down a date as a parameter
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER Procedure [dbo].[sp_tblEvents_Search_sel]
@Ev_C_Id int =0,
@Ev_Tipo int =0,
@Evd_Date datetime = '1/1/1900'
As
Begin
SET NOCOUNT ON;
declare @swhere varchar(4000)
declare @sSQL varchar(5000)
set @swhere = COALESCE(@swhere + ' ','')
set @sSQL = COALESCE(@sSQL + ' ','')
if ( @Evd_Date <>'1/1/1900')
begin
set @swhere = @swhere + ' AND DATEDIFF(d, GETDATE(), ' + cast(@Evd_Date as datetime) + ') =0 '
end
if (@Ev_C_Id<>0)
begin
set @swhere = @swhere + ' AND Ev_C_Id=' + CONVERT(nvarchar(11), @Ev_C_Id)
end
if (@Ev_Tipo<>0)
begin
set @swhere = @swhere + ' AND Ev_Tipo=' + CONVERT(nvarchar(11), @Ev_Tipo)
end
set @sSQL = 'SELECT Ev_C_Id, Ev_Tipo,S_Name, Sd_Title, S_Foto, Sd_LanguageId, C_Name, Ev_Name, Ev_Hour, Ev_Location, Ev_Fee, Ev_Description, iDays, Sd_Id, Evd_Date, S_Logo, Ev_DisplayUntil, Evd_Id, Ev_URL,S_Id, sType,S_Premium, S_PremiumDate FROM vwEventsDates WHERE Ev_Estado=1 ' + @swhere + ' ORDER BY Evd_Date '
exec(@sSQL)
End
The issue is here
set @swhere = @swhere + ' AND DATEDIFF(d, GETDATE(), ' + cast(@Evd_Date as datetime) + ') =0 '
When running the sp it says "Conversion failed when converting date and/or time from character string."
Any clue ?
jean-luc
www.corobori.com