I recently discovered a table in our Sybase database at work that uses a column of a type 'timestamp'. If I create a table using this mysterious timestamp datatype like this
create table dropme (
foo timestamp,
roo int null
)
insert into dropme (roo) values(123)
insert into dropme (roo) values(122)
insert into dropme (roo) values(121)
select * from dropme
go
I get the following from 'select * from dropme':
foo roo
-------------------- -----------
0x000100000e1ce4ea 123
0x000100000e1ce4ed 122
0x000100000e1ce509 121
0x000100000e1ce4ea does not look very timestampy to me. Also, I see this output from 'sp_help timestamp':
Type_name Storage_type Length Prec Scale Nulls Default_name Rule_name Access_Rule_name Identity
--------- ------------ ------ ---- ----- ----- ------------ --------- ---------------- ----------
timestamp varbinary 8 NULL NULL 1 NULL NULL NULL NULL
The TIMESTAMP data type is used for values that contain both date and time parts. TIMESTAMP has a range of '1970-01-01 00:00:01' UTC to '2038-01-19 03:14:07' UTC. A DATETIME or TIMESTAMP value can include a trailing fractional seconds part in up to microseconds (6 digits) precision.
Timestamp is a synonym for rowversion. Rowversion data type is not a date or time data type. Each database has a counter that is incremented for each insert or update operation that is performed on a table that contains a rowversion column within the database. This counter is the database rowversion.
When converting date data to a character type, use style numbers 1 – 7 (101 – 107) or 10 – 12 (110 – 112) in Table 16-5 to specify the display format. The default value is 100 (mon dd yyyy hh:miAM (or PM )).
This SSIS expression (e.g. for use in a variable or derived column) returns a YYYYMMDDHHMMSS style 14-character timestamp from a datetime. This example uses the current date and time; for other datetimes replace getdate() with the variable/column/expression containing your required value.
What the heck is a timestamp?
The timestamp datatype is defined as
varbinary(8) null
Does it have any relation at all to time or date?
No. The name was poorly chosen.
Can I convert it to a datetime?
No.
If its not a time or a date, what do you use it for?
Each time a row with a timestamp column is inserted or updated, the timestamp column is updated automatically. Note that there are actually two kinds of timestamps. TIMESTAMP and CURRENT TIMESTAMP. The difference is that CURRENT TIMESTAMP is only set on insert.
The Sybase documentation stopped there leaving me wondering why the f*rainbow!*k anyone would ever use the datatype timestamp. Happily, I found some other discussions and deduced its used when implementing optimistic concurrency control.
Concurrency control is a method of ensuring that multiple transactions can run at/around the same time and still result in correct data. Optimistic concurrency control is a concurrency control method that assumes multiple transactions can complete without interfering with each other. Ie no locking is required. Wikipedia describes the following algorithm:
Sybase's timestamp datatype could be used in steps 1 and 3 of this algorithm instead of using a date/time. But it doesn't seem to me like it saves you much work over using a datetime datatype. I suppose it might perform better.
Recently, somebody asked me if it is possible to convert the TIMESTAMP SYBASE IQ data type to DATE; I have always avoided this data type because it's darkness. After some pair of hours of reading SYBASE documentation and making some tests, here are my conclusions:
The TIMESTAMP:
Here is the SQL sentence for converting the TIMESTAMP to DATE:
SELECT timestamp as TS, CONVERT(decimal, timestamp) as TS_IN_MS,
CONVERT(date, dateadd(SS, CONVERT(int, SUBSTRING(CONVERT(varchar,
CONVERT(decimal, timestamp)), 1, 9)), '1/1/1970'), 123) as TS_AS_DATE
FROM TheTable
The conversion can be proved by using an online EPOCH converter like the following:
Note: In the case of SYBASE ASE, the TIMESTAMP type is not a valid UNIX-EPOCH.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With