如何解决转换nvarchar值时转换失败?

时间:2009-05-12 12:25:58

标签: sql sql-server sql-server-2005

CREATE TABLE #Temp (VisitingCount int, [Time] int, [Date] nvarchar(50) )
DECLARE @DateNow DATETIME,@i int,@Time int, @Date nvarchar(50)
set @DateNow='00:00'  
set @i=1;  
while(@i^60;48)  
    begin  
        set @DateNow = DATEADD(minute, 30, @DateNow)
        set @Time = (datepart(hour,@DateNow)*60+datepart(minute,@DateNow))/30 
        set @Date = CONVERT(VARCHAR(5),@DateNow, 108)
        insert into #Temp(VisitingCount,[Time],[Date]) values(0,@Time,@Date )
        set @i=@i+1
    end
select Sum(VisitingCount)as VisitingCount, [Time],Date
from (
  select Sum(VisitingCount)as VisitingCount, [Time],Date
    from #Temp group by [Time],Date
  Union All
    select count(page) as VisitingCount,CONVERT(VARCHAR(5),Date, 108), 
    (datepart(hour,Date)*60+datepart(minute,Date))/30 as [Time]
    from scr_SecuristLog
    where Date between '2009-05-12' and '2009-05-13'
    GROUP BY (datepart(hour,Date)*60+datepart(minute,Date))/30,CONVERT(VARCHAR(5),Date, 108)
  ) X
group by [Time],Date
order by 2 asc  

错误

Msg 245,Level 16,State 1,Line 1 将nvarchar值'00:30'转换为数据类型int时,转换失败。

错误区域:选择Sum(VisitingCount)作为VisitingCount,[Time],Date .................

1 个答案:

答案 0 :(得分:4)

在你的联合的第二部分中,使用的数据类型是(int,date,time),而在第一部分中它们是(int,time,date)。尝试更改联合的第一部分或第二部分中列的顺序。