Use Datediff with update query.An aggregate may not appear in the set list of an UPDATE statement

StackOverflow https://stackoverflow.com/questions/23671892

  •  23-07-2023
  •  | 
  •  

Question

I am using this query to update Hours according to two datetime values"

UPDATE CheckInCheckOut 
SET CountHours=ROUND(SUM(DATEDIFF(ss,'2014-05-15 00:23:04.580',
'2014-05-15 00:25:04.580') / 60.0 / 60.0), 2) 
FROM CheckInCheckOut 

Here CountHours is decimal(18,0)

DateDiff workes if I use select but gives error on update..?

Any example would be appreciated.

Was it helpful?

Solution

Problem:

You are adding from CheckInCheckOut at the end of the UPDATE statement.

try this:

update CheckInCheckOut 
set CountHours=ROUND(SUM(DATEDIFF(ss,'2014-05-15 00:23:04.580','2014-05-15 00:25:04.580')  / 60.0 / 60.0), 2) 

Here is UPDATE syntax:

UPDATE table_name
SET column1=value1,column2=value2,...
WHERE some_column=some_value;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top