Quantcast
Channel: Forum Getting started with SQL Server
Viewing all articles
Browse latest Browse all 7129

using date within dynamic query

$
0
0

Hello ,

 I write the following dynamic query  :

  declare @FromDate date ='2014-01-20'
  declare @ToDate date ='2014-01-28'

 declare @EmployeeId int =1
 declare @query  AS NVARCHAR(MAX)

set @query = 'select Project.ProjectName , Task.TaskTitle , TimeSheet.TaskDate , 
(select sum(TimeSheet.DurationInHours) from TimeSheet  
 where TimeSheet.TaskId = Task.TaskId And TimeSheet.TaskDate between '+@FromDate+' and '+@ToDate+') as TotalHours
from TimeSheet 
inner join Task on TimeSheet.TaskId = Task.TaskId
inner join Project on Project.ProjectId = Task.ProjectId
where TimeSheet.EmployeeId='+CAST(@EmployeeId as varchar(max))+'
) as SourceTable'

execute(@query)

but I get the following error :

  The data types varchar(max) and date are incompatible in the add operator.

 How Can I solve it ?


ASk



Viewing all articles
Browse latest Browse all 7129

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>