dynamic sql query syntax

Hi friends
I need ur help with syntax of following dynamic sql.actually it is simple version of what am doing but i know for sure this line actually causing the error.

DECLARE @p_taskentrydtfilter nvarchar(50),@taskentrydttag nvarchar(100)
declare @p_taskentrydt DATETIME

set @p_taskentrydtfilter= '>='
set @p_taskentrydt = '20050609'

select @taskentrydttag=case when (@p_taskentrydt='' OR @p_taskentrydt is null) then '' else
' and task.entrydt '+
@p_taskentrydtfilter+''+@p_taskentrydt+''' and task.entrydt <'''+dateadd(d,1,@p_taskentrydt)+'' end

as u can see am storing a CASE stmt in a variable but it gives me error saying
"Syntax error converting datetime from character string."
any ideas .Thanks

Cheers

[861 byte] By [prk] at [2008-2-23]
# 1

Hi,

I think its your date format. try setting your @p_taskentrydt to other date date formats such as mm/dd/yyyy

cheers,

Paul June A. Domag

PaulDomag at 2007-9-9 > top of Msdn Tech,SQL Server,Transact-SQL...
# 2
@p_taskentrydtfilter+''+@p_taskentrydt + '''

should be:

@p_taskentrydtfilter+''''+@p_taskentrydt + '''

But you should be using sp_executesql instead of dynamically concatenating variables that can be set by caller for example. See BOL for more details on how to use sp_executesql to execute parameterized SQL statements.

# 3
Thanks Umachandar
That worked nicely Smile
prk at 2007-9-9 > top of Msdn Tech,SQL Server,Transact-SQL...

SQL Server

Site Classified