Part of an app I am working on involves finding the time difference between times.
One of my database tables includes a field which has times in the following format: for example:
01-01-1900 14:35:00
01-01-1900 15:45:00
The problem I have is that I want to find the difference between say 14:35:00 in my database table and 29-12-2007 14:45:15.
When I use DATEDIFF this falls because of the huge difference between the Date parts of these values.
Is there any way I can update just the date parts of a DateTime field. So for instance (today) my two examples would become:
29-12-2007 14:35:00
29-12-2007 15:45:00
I have tried using DATEPART to extract the day, month and year of the current date and then 'rebuild' these dates using an UPDATE statement but this did not work.
I also tried the following which takes the time from each record and then attempts to update the relevant field with the current date and the correct time for the record.
This just displays a stupid date though because sql is attempting to ADD the current date and the time.Code:DECLARE @currentPK int SET @currentPK = 1 DECLARE @maxPK int SELECT @maxPK = MAX(CallChargePK) FROM InternetTracking_Data.dbo.tblCallCharges DECLARE @tempTime datetime DECLARE @time varchar(15) while (@currentPK <= @maxPK) begin SELECT @tempTime = StartTime FROM InternetTracking_Data.dbo.tblCallCharges WHERE CallChargePK = @currentPK UPDATE InternetTracking_Data.dbo.tblCallCharges SET StartTime = getDate() + ' ' + @tempTime SET @currentPK = @currentPK + 1 end




Reply With Quote