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?
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
To identify new rows by timestamp you need to keep track of the timestamps that were there beforehand. In a pinch you could:
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).
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:
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.
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:
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.
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