Is there a way to get DateTime value from timestam

2020-02-01 07:10发布

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?

8条回答
Fickle 薄情
2楼-- · 2020-02-01 07:52

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

查看更多
爷、活的狠高调
3楼-- · 2020-02-01 07:56

To identify new rows by timestamp you need to keep track of the timestamps that were there beforehand. In a pinch you could:

  • Restore a previous version somewhere else.
  • Copy the data from both tables into a scratch database.
  • Identify the inserted data from the timestamps present in one but not the other.

With a minor risk of false positives if anything else has been going on in the DB this will get you a reasonably good difference.

For a more robust check you could calculate MD5 or SHA-1 hashes with Hashbytes on the row contents to give you a difference with a very low probability of collision (see this wikipedia article on Birthday attacks for a discussion of this problem).

查看更多
够拽才男人
4楼-- · 2020-02-01 07:57

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.

查看更多
戒情不戒烟
5楼-- · 2020-02-01 07:58

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
查看更多
对你真心纯属浪费
6楼-- · 2020-02-01 07:59

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.

查看更多
迷人小祖宗
7楼-- · 2020-02-01 08:03

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.

查看更多
登录 后发表回答