Click here to Skip to main content
15,884,986 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
My stored procedure is as follows,

SQL
 -- Add the parameters for the stored procedure here
@FromDate datetime,
@ToDate datetime

    --Select query
   DECLARE @query nvarchar(max)

   set @query='SELECT [col1]
               FROM [Table1]
               WHERE ([col2] BETWEEN '''+@FromDate+''' AND'''+@ToDate+''')'

    execute sp_executesql @query


Executing this string query results in following error,

"Conversion failed when converting date and/or time from character string"

Any one please help me to sort this out
Posted
Updated 7-Apr-13 5:18am
v2
Comments
[no name] 7-Apr-13 11:38am    
Post the code that you have for calling this stored procedure.

Hi, You can try this

SQL
@FromDate datetime,
 @ToDate datetime

     --Select query
    DECLARE @query nvarchar(max)

    set @query='SELECT [col1]
                FROM [Table1]
                WHERE ([col2] BETWEEN '''+CONVERT(NVARCHAR, @FromDate,21)+''' AND'''+CONVERT(NVARCHAR, @ToDate, 21)+''')'

     execute sp_executesql @query
 
Share this answer
 
v2
Your query looks OK...

Try this:
SQL
set @query=N'SELECT [col1] 
           FROM [Table1] 
           WHERE ([col2] BETWEEN ''' + @FromDate + ''' AND ''' + @ToDate + ''')'


Check the input parameters by printing its content to MS SQL Managment Studio - Messages window:
SQL
PRINT @FromDate
PRINT @FromDate
 
Share this answer
 
v2

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