I have a table that has a DATETIME
name type field AppointmentDate
where it saves data with format 2022-07-27 21:32:54.420
and another field of TIME
name type EndHour
and saves data with format 13:00:00.0000000
.
So you needed a query to update the records where the date is Less than or Equal to field AppointmenDate
and Greater than field EndHour
.
I implemented this code, but it's not working for me, because what I needed was for it to compare AppointmentDate
only the date part against the field and EndHour
only the time part against the field. (I thought that only comparing AppointmentDate against the current date would do it but it doesn't work, you should also include the comparison against EndHour but I don't know how to do it)
UPDATE ScheduledVirtualAppointments SET IdState = @idStateDone , DateUpdate = GETDATE(), IdUserUpdate = 1
WHERE AppointmentDate < GETDATE() AND
IdState NOT IN (@idStateDone,@idStateRejection,@idStateCancellation,@idStateQuizAnswers)
How can i fix this?
According to your question what you are looking for is to filter in the update:
AppointmentDate
than or equal to todayEndHour
You could use
CAST()
to keep each part of a date/time:Note: We use a variable
@Now
for the current date since itGETDATE()
is a non-deterministic function, that is, each execution will surely return a different value and since it must be used more than once in the query, it is preferable to always handle the same date/time.Thank you Patricio Moracho.
The complete code looks like this: