Is there a way to get DateTime value from timestamp type column?

50,999

Solution 1

The Transact-SQL timestamp data type is a binary data type with no time-related values.

So to answer your question: Is there a way to get DateTime value from timestamp type column?

The answer is: No

Solution 2

The timestamp datatype in SQL Server 2005 is a synonym of rowversion and is just a number that is automatically incremented with each row update.

You can cast it to bigint to see its value.

To get what you want for new or updated rows, you should propably add another datetime column (lastupdate) and a trigger to update that column with each update.

For rows that have already been inserted in the past I don't think that you can do something to find the exact time.

Solution 3

I'm afraid it's not possible to convert/cast a TIMESTAMP to a DATETIME. They have entirely different uses and implementations that are incompatible.

See this link http://www.sqlteam.com/article/timestamps-vs-datetime-data-types

Books on-line also says http://msdn.microsoft.com/en-us/library/aa260631.aspx

The SQL Server timestamp data type has nothing to do with times or dates. SQL Server timestamps are binary numbers that indicate the relative sequence in which data modifications took place in a database. The timestamp data type was originally implemented to support the SQL Server recovery algorithms.

Solution 4

Another answer to you question:

If the timestamp column is the only resource for you recovery (no backups etc) you may try to use the following logic

Timestamp is simply a value of a counter that is incremented for each insert or update operation that is performed on a table that contains a timestamp column.

If the data import that happened yesterday was one insert of several records you may see a sequence of numbers in the timestamp column like e.g:

0x00000000000007D1
0x00000000000007D2
0x00000000000007D3
0x00000000000007D4
0x00000000000007D5

The most recent sequence can be your added data (of course it is not guarantied) You con combine that knowledge with other things (like auto-increment column if you use them) to identify the records you are interested in.

Solution 5

Other people correctly pointed out that the timestamp is a binary counter. Nevertheless, if in any table of your database, you have the timestamp and the datetime when it was recorded, you can use that piece of information to go from any timestamp to a date range. A log table is a good candidate for this purpose. Assuming your import table is "invoices", you could use a query like the following:

WITH TS 
AS
(
SELECT 
    L1.LastDateUpdated, COALESCE(L2.LastDateUpdated, {TS '2099-12-31 00:00:00'}) as LastDateUpdatedTo,
    L1.[TIMESTAMP], L2.[TIMESTAMP] as [TIMESTAMPTo]
FROM 
(
    SELECT L1.[LastDateUpdated]
          ,L1.[TIMESTAMP]
          ,ROW_NUMBER() OVER (ORDER BY L1.[LastDateUpdated]) ID
    FROM [Log] L1
) L1
left join 
(
    SELECT L2.[LastDateUpdated]
          ,L2.[TIMESTAMP]
          ,ROW_NUMBER() OVER (ORDER BY L2.[LastDateUpdated]) ID
    FROM [Log] L2
) L2 
    ON L1.ID = L2.ID - 1
)
SELECT TS.LastDateUpdated, TS.LastDateUpdatedTo, * from [Invoices]
    inner join TS ON [Invoices].Timestamp between TS.Timestamp and 
TS.TIMESTAMPTo
ORDER BY TS.TIMESTAMPTo DESC
Share:
50,999
Dandikas
Author by

Dandikas

C#, Agile

Updated on April 27, 2020

Comments

  • Dandikas
    Dandikas about 4 years

    I need a select from table which does not have column that tells when row was inserted, only timestamp column (values like: 0x0000000000530278). Some data was imported to the table yesterday and now I need to find out what exactly was imported :(

    Is there a way to do it using only timestamp info? Here I found that:

    • Timestamp is a 8 bytes sequential Hex number, that has nothing to do with neither the date nor the time.
    • To get the current value of timestamp, use: @@DBTS.

    Perhaps there is a way to find what was timestamp value around specific time? That would help to form a select. Or maybe there is a well known solution?

  • Dandikas
    Dandikas over 15 years
    That explains timestamp column type, but does not answer my question :|
  • Dandikas
    Dandikas over 15 years
    Perhaps there is a way to find what was timestamp value around specific time? Import is a rear thing, so time gap is significant (precision is not needed)