Hy,
I have a problem and I can’t figure it out…
declare @start_date datetime, @date_1m datetime, @date_1m_end datetime
set @start_date = GETDATE()
set @date_1m = DATEADD(MONTH,-1,DATEADD(MONTH, DATEDIFF(MONTH, 0, @start_date),0))
set @date_1m_end = DATEADD(SS, -1, DATEADD(MONTH, 1, @date_1m))
set @sql_exec = 'insert into #temp select ... where a.date between ' + @date_1m + ' and ' + @date_1m_end + ''
exec(@sql_exec)
and it gives me the following error :
Conversion failed when converting date and/or time from character string.
Why and how to solve this because it’s getting frustrating ..
PS: I need to run exec() because this stored procedure will run every month and it creates database for that month
Thanks you very much
TSql is trying to convert all your text to datetime. You need to convert dates to strings before concationation, and convert them back to dates in query.