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

后端 未结 8 686
执笔经年
执笔经年 2020-12-06 17:30

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 i

相关标签:
8条回答
  • 2020-12-06 17:46

    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.

    0 讨论(0)
  • 2020-12-06 17:54

    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
    
    0 讨论(0)
  • 2020-12-06 17:57

    I think your best bet is to restore a backup from before the inserts and compare the backuped table with the current table.

    0 讨论(0)
  • 2020-12-06 17:58

    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.

    0 讨论(0)
  • 2020-12-06 18:00

    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.

    0 讨论(0)
  • 2020-12-06 18:05

    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

    0 讨论(0)
提交回复
热议问题