I have written a simple procedure:
CREATE PROCEDURE [dbo].[sp_GetPublishedDocs2]
@FromDate as Datetime
AS
BEGIN
DECLARE @strSQL VARCHAR(5000)
SET @strSQL='SELECT * From Task WHERE 1=1 '
IF @FromDate <>'1/1/1900'
SET @strSQL = @strSQL + ' AND Task.CreatedDate >= '+Cast(@FromDate as Datetime)
EXEC(@strSQL)
END
It run successfully when I pass parameter ‘1/1/1900’ however when I pass any other date it says: Conversion failed when converting date and/or time from character string.
Is there anyone.. who could help me…
Thanks in Advance.
You should cast the @FromDate into a varchar since you are doing a string concat.
Try to avoid
SELECT * FROM. It is faster to define all columns explicitly.HINT: for testing you could use
PRINTfavor ofEXECto see what sql has been produced.EDIT: You might use
VARCHAR(MAX)here…