I wrote this query:
declare @startdate nvarchar(10) = '2016/01/01',
@enddate nvarchar(10) = '2019/10/04',
@cols nvarchar(max),
@strsql nvarchar(max);
select
@cols =
(select distinct QUOTENAME(maintenancename)+','
from (select distinct maintenancename
from Table_maintenancetype where coalesce(maintenancename, '') != '') AS t1
for xml path('')
)
set @cols=LEFT(@cols,LEN(@cols)-1)
set @strsql=N'select *
from
(select sum(timedurationok) as wt, maintenancetype,tcode
from Table_maintenancereport
where ((svok=1 and need_netok=0) or (svok=1 and netok=1 and need_netok=1))and tcode<>-1
and dateendrole >='+ @startdate+ 'and dateendrole<='+ @enddate+'
group by maintenancetype,tcode
) d
pivot
(sum(wt)
for maintenancetype in ('+@cols+')
) piv
'
exec sp_executesql @strsql
When execute this no record return but when replace @startdate
and @enddate
by '2016/01/01','2019/10/04'
The query works fine I don't know what the problem is... and when use between in query I get error for can not convert nvarchar to data int 2016