Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does a timestamp in T-Sql mean in C#?

I'm trying to develop a model object to hold a Sql Server row, and I understand perfectly how to do this except for the T-Sql/SqlServer timestamp. The table is defined as:

CREATE TABLE activity (
activity_id int
, ip_address varchar(39)
, user_id varchar(255)
, message_text
, dt timestamp
)

When I resolve a table row to my object, for an int or a string I would expect to do something like:

ActivityID = (int)dataReader["activity_id"];
IPAddress = (string)dataReader["ip_address"];

But what do I do about the timestamp column? There's no "timestamp" datatype that I can find anywhere. I know that Sql Server stores timestamp as an 8 byte binary, but what is this normally the equivalent of in .NET?

Edited to add: A little extra information... this is a row being returned from a DB2 table on our mainframe coming through a Sql Server view. "Rowversion" isn't an option, and DB2 is handing the column off as a timestamp. If timestamp and rowversion are identical, maybe I can treat it as one, but otherwise I'm stuck with timestamp.

Edited again to add: This project is going to drive me nuts. It will, at least, be a short trip. Anyway, yes @JoelC this is a Sql Server view into a DB2 database on the mainframe. I was finally able to track down one of our DBAs, who explained disdainfully that "of course" a DB2 TIMESTAMP comes across as to a Sql Server view as a datetime. From his tone of voice I guess only noobs don't know this. That's why he named it "datetime" in the actual view, Duh! (I gave it a different name in my example so as to not trigger commentary on naming conventions -- the actual data model diagram says it's a TIMESTAMP and names it timestamp). So, in this event, apparently one must cast it to a DateTime. I think I may begin considering becoming a DBA so that I, too, can drive programmers crazy. Sorry if I misled any of the responders to this question -- it was unintentional, as I was actually expecting a timestamp to be, well, a timestamp. Silly me. Thanks are especially due to Microsoft for naming a byte-array datatype a "timestamp" when it has nothing to do with dates and times. I haven't the vaguest idea which response to mark as the Answer. Sigh.

like image 781
Cyberherbalist Avatar asked Jun 13 '11 18:06

Cyberherbalist


People also ask

What does timestamp mean in SQL?

Timestamp in Standard Structured Query Language (SQL) is a data type, that basically represents the date and time value of any specific record at any specific time (or event). Timestamps in SQL can be used to store and work with dates, times, time zones, and AD/BCs.

How is timestamp stored in SQL?

The format of a TIMESTAMP is YYYY-MM-DD HH:MM:SS which is fixed at 19 characters. The TIMESTAMP value has a range from '1970-01-01 00:00:01' UTC to '2038-01-19 03:14:07' UTC . When you insert a TIMESTAMP value into a table, MySQL converts it from your connection's time zone to UTC for storing.


1 Answers

According to this post

you'll have to cast it to a byte array.

byte[] dt = dataReader["dt"] as byte[];
like image 107
Bala R Avatar answered Oct 16 '22 17:10

Bala R