February 1, 2007 at 10:23 am
Hi,
I have a TimeStamp field and from it I am creating Min(TimeStamp) and Max(Timestamp)
And now I want the difference in Time (hrs or minutes) between them.
I tried datediff(mi,Min(TimeStamp),Max(TimeStamp)) but to no avail.
Any ideas please?
February 1, 2007 at 11:41 am
You can't do that. You need a date column. Timestamp, contrary to his name, has no association with date nor time.
February 1, 2007 at 11:48 am
Ninja is correct. You can convert the value to an integer and attempt seeing the difference in that fashion.
Here is a query showing a 24 second difference and the different actual values you will return:
DECLARE @TimeStamp AS timestamp
DECLARE @Time TABLE( TimeStampValue timestamp,
Counter integer)
DECLARE @Counter integer
SET @Counter = 1
WHILE @Counter <= 25
BEGIN
INSERT INTO @Time( Counter) SELECT @Counter
WAITFOR DELAY '000:00:01'
SET @Counter = @Counter + 1
END
SELECT MIN( TimeStampValue), MAX( TimeStampValue) FROM @Time
SELECT MIN( CONVERT( datetime, TimeStampValue)), MAX( CONVERT( datetime, TimeStampValue)) FROM @Time
SELECT MAX( CONVERT( integer, TimeStampValue)) - MIN( CONVERT( integer, TimeStampValue)) FROM @Time
I wasn't born stupid - I had to study.
February 2, 2007 at 12:33 pm
If you use a column with date values in it, you can do the query you are trying to do:
Declare
@test-2 Table (MyID int identity primary key not null, MyDate datetime)
Insert
Into @test-2 (MyDate) Select '1/1/2000 4:51:00 AM'
Insert
Into @test-2 (MyDate) Select '1/1/2000 4:52:00 AM'
Insert
Into @test-2 (MyDate) Select '1/1/2000 4:53:00 AM'
Select
Min(MyDate) as MyMinDate, Max(MyDate) As MyMaxDate, DateDiff(mi, Min(MyDate), Max(MyDate)) As MyDateDifference
From
February 4, 2007 at 6:24 pm
John,
What is the data type of the column you are calling "TimeStamp"?
--Jeff Moden
Change is inevitable... Change for the better is not.
February 4, 2007 at 8:09 pm
Have a look at "Timestamp data type" in books online.
In part, this says...
timestamp is a data type that exposes automatically generated binary numbers, which are guaranteed to be unique within a database. timestamp is used typically as a mechanism for version-stamping table rows. The storage size is 8 bytes.
February 5, 2007 at 11:29 am
A timestamp is not simply a randomly generated number. It has to be based upon some value or point in time - otherwise it would be like a GUID or a RAN() with a seed that is based upon some value or point in time...
The following query does show that the timestamp value, when converted to an integer, can give the user seconds, (which obviously can be converted to another time measurement).
I have not been able to find how SQL Server actually generates the timestamp value. Can someone answer this? TIA
DECLARE @Time TABLE( TimeStampValue timestamp,
Counter integer)
DECLARE @Counter integer
SET @Counter = 1
WHILE @Counter <= 5
BEGIN
INSERT INTO @Time( Counter) SELECT @Counter
WAITFOR DELAY '000:00:01'
SELECT TimeStampValue,
CONVERT( integer, TimeStampValue) AS 'TimeStamp',
GETDATE() AS 'GETDATE()'
FROM @Time WHERE Counter = @Counter
SET @Counter = @Counter + 1
END
I wasn't born stupid - I had to study.
February 5, 2007 at 11:42 am
That query only appears to represent secodns because you put a 1 second delay in there. Comment out the delay and you will see that the timestamp increments each time whereas the date value does not.
A timestamp is nothing more than a number that increments by 1 every time you use it. That's it. No date or time values can be derived directly from a timestamp value!!!
Unlike an identity column, there is only 1 timestamp entity per database and you can therefore be guaranteed that a timestamp value will always be unique within a database.
February 5, 2007 at 1:16 pm
Thanks! I just could not get it through my thick head...
I wasn't born stupid - I had to study.
February 5, 2007 at 7:15 pm
Nicely done, Robert.
I'm still waiting for the original poster to definitely describe the datatype of the column he named "TimeStamp" (instead of guessing ).
--Jeff Moden
Change is inevitable... Change for the better is not.
February 6, 2007 at 2:23 am
Hi everybody,
blimey, thanks for your help. It's all getting a bit beyond me now. I'm gonna have to spend a bit of time digesting all this.
Jeff, the datatype is a set as Datetime. Thanks
Many thanks to you all
John
February 8, 2007 at 1:18 pm
Just tried this and it seems to work fine. Here is my query:
select
datediff(mi,Min(TimeStamp),Max(TimeStamp)) from GrowthDetails
Timestamp column is in this case a datetime field.
Viewing 12 posts - 1 through 11 (of 11 total)
You must be logged in to reply to this topic. Login to reply