SQL转换无法插入参数-date

时间:2015-07-06 11:59:50

标签: sql sql-server tsql

我正在尝试将参数日期放入临时表中。 但总是让转换失败。

有关于datetime转换的建议吗? 感谢

--***********************************************************************************************************
Declare @dblQuote as varchar(4)
Declare @sDate as varchar(10)

Declare @sSQL_tmpHCDetails nvarchar(max)


Select @dblQuote = ''''

-- #tmpHCDetails  ***********************************************************************************************************
Declare @StartDate  datetime ='7/6/2015 12:00:00 AM'

IF IsNull(Object_Id('tempdb..#temptb'),0) <> 0 DROP TABLE #temptb; 

create table #temptb
(
 sDate  datetime
);


set @sSQL_tmpHCDetails='INSert into #temptb ( sDate ) '
set @sSQL_tmpHCDetails=@sSQL_tmpHCDetails+ 'select Convert(DateTime,'+ @dblQuote + @StartDate + ' 23:23:59 ' + @dblQuote + ',108)'



exec @sSQL_tmpHCDetails

select * from   #temptb 

1 个答案:

答案 0 :(得分:0)

Your Solution....

Declare @dblQuote as varchar(4) Declare @sDate as varchar(10)

Declare @sSQL_tmpHCDetails nvarchar(max)

Select @dblQuote = ''''

-- #tmpHCDetails *********************************************************************************************************** Declare @StartDate datetime ='7/6/2015 12:00:00 AM'

SELECT CONVERT(VARCHAR(20),@StartDate,107)

IF IsNull(Object_Id('tempdb..#temptb'),0) <> 0 DROP TABLE #temptb;

create table #temptb ( sDate datetime );

set @sSQL_tmpHCDetails='INSert into #temptb ( sDate ) '

set @sSQL_tmpHCDetails=@sSQL_tmpHCDetails+ 'select Convert(DateTime,'+ @dblQuote + CONVERT(VARCHAR(20),@StartDate,107) + ' 23:23:59' + @dblQuote + ',108)'

exec(@sSQL_tmpHCDetails) select * from #temptb