仅更新SQL Server 2008中的datetime列的时间部分

时间:2013-11-16 15:04:24

标签: sql sql-server datetime

UPDATE tblAttendance2 
SET 
    DateTimeIn = CONVERT(varchar(10), DateTimeIn, 120) + @TimeInChange,
    DateTimeOut = CONVERT(varchar(10), DateTimeIn, 120) + @TimeOutChange
WHERE 
    AttendanceDeductionsID = @AttendanceDeductionsID

我收到此错误

  

Msg 402,Level 16,State 1,Procedure SP_UpdateDTR_TimeChange,Line 8
  数据类型varchar和time在add运算符中不兼容。

3 个答案:

答案 0 :(得分:0)

您正在将DateTimeIn转换为varchar(),然后添加@TimeInChange(可能是Time类型)。这不起作用。

如果DateTimeIn的类型为DateTime,而@TimeInChange的类型为Time,那么这将起作用:

UPDATE tblAttendance2 
SET DateTimeIn = DateTimeIn + cast(@TimeInChange as DateTime),
DateTimeOut =DateTimeIn + cast(@TimeOutChange as DateTime)
WHERE AttendanceDeductionsID = @AttendanceDeductionsID

答案 1 :(得分:0)

假设@timeInChange的数据类型为time,请尝试使用

UPDATE tblAttendance2 
SET 
    DateTimeIn  = cast(cast( DateTimeIn as date) as datetime) +  cast (@TimeInChange as datetime),
    DateTimeOut = cast(cast( DateTimeIn as date) as datetime)  + cast (@TimeOutChange as datetime)
WHERE 
    AttendanceDeductionsID = @AttendanceDeductionsID

答案 2 :(得分:0)

请尝试:

Sql Server如果您只需要hh + mm,那么您可以

--DECLARE @datetimein datetime

--SELECT @datetime = GETDATE()



UPDATE tblAttendance2 
SET DateTimeIn =
 CAST(DATEPART(hour, @datetimein) as varchar(2)) + 
':' +  CAST(DATEPART(minute, @datetimein)as varchar(2)) +@TimeInChange,

Date time out =use same procedure as above + @timeoutchange

  where <some condition condition>