I am getting error "Explicit conversion from data type int to date is not allowed." while trying to execute the code. The SQL is
USE [xxDB]
GO
declare @currentMonth int = 1,
@currentYear int = 2014,
@newExpireMonth int =6,
@newExpireYear int =2018,
@actualDate varchar(30),
@fromDate varchar(30),
@toDate varchar(30)
declare @Sql varchar(8000)
begin
create table #MytempTab1 
        (
            actualNewDate varchar(30), 
            newUserId int              
        )
  insert into #MytempTab1
   select 
    (RIGHT( '00' + CAST(ISNULL(ss.ExpiryMonth, '01') as varchar(2)), 
          2)+'-01'+'-'+RIGHT('0000' + CAST(ISNULL(ss.ExpiryYear, '2014') as 
          varchar(4)), 4)) as actualNewDate, ss.UserId as newUserId
    from
    dbo.UserProfile u
    inner join dbo.webpages_UsersInRoles wur on u.UserId=wur.UserId
    inner join dbo.SpreedlySubscriber ss on u.UserId=ss.UserId      
  where 
    (wur.RoleId=4) 
end
  set @Sql= '
 select
  ss.FirstName as FirstName, ss.LastName as LastName,   
  ss.ExpiryMonth as ExpMonth, ss.ExpiryYear as ExpYear 
from dbo.UserProfile u
inner join dbo.webpages_UsersInRoles wur on u.UserId=wur.UserId
inner join dbo.SpreedlySubscriber ss on u.UserId=ss.UserId
inner join #MytempTab1 mt on ss.UserId= mt.newUserId
inner join dbo.[Order] odr on ss.OrderId=odr.OrderId
where (wur.RoleId=4) and '
set @fromDate =  RIGHT('00' + CAST(@currentMonth  as varchar(2)), 2) +'-01'+'-'+ RIGHT('0000' + CAST(@currentYear as varchar(4)), 4)
set @toDate=  RIGHT('00' + CAST(@newExpireMonth as varchar(2)), 2) +'-01'+'-'+ RIGHT('0000' + CAST(@newExpireYear as varchar(4)), 4)
set @Sql= @Sql+ '(CONVERT(date, mt.actualNewDate, 110) BETWEEN CONVERT(date, '+@fromDate+', 110) AND CONVERT(date, '+@toDate+', 110))'
 print (@Sql)
 EXEC(@Sql)
 drop table #MytempTab1
I believe it is the last line which is comparing the dates. The temporary table only crating the string and this last line converting it to date and comparing.
Thanks in advance.
 
    