{"id":1102,"date":"2005-07-30T04:07:00","date_gmt":"2005-07-30T09:07:00","guid":{"rendered":"http:\/\/blogs.devhorizon.com\/reza\/?p=1102"},"modified":"2005-07-30T04:07:00","modified_gmt":"2005-07-30T09:07:00","slug":"timestamp-vs-datetime-data-types-in-sql-server","status":"publish","type":"post","link":"https:\/\/blogs.devhorizon.com\/reza\/2005\/07\/30\/timestamp-vs-datetime-data-types-in-sql-server\/","title":{"rendered":"TimeStamp VS DateTime data types in SQL SERVER"},"content":{"rendered":"<p><P>Couple years ago I was struggling a bit with timestamp (Also known as ROWVERSION) and DateTime data types in Sql Server as I wanted to have the date and time of when each record in Table is modified in my application. Yesterday I saw couple of posts in the newsgroups in this regards so I decided to explain it again here for those who have the same issue.<\/P><br \/>\n<P>If you want to have the last modified date and time of each record then you have two options:<\/P><br \/>\n<P>1) Define the field as datetime in your database and then in your stored procedure use something like this:<\/P><br \/>\n<P>CREATE PROC NWtest <BR>@endDate datetime = NULL<BR>AS<BR>IF @enddate IS NULL<BR>SET @endDate = GetDate()<BR>SELECT OrderDate<BR>FROM Northwind.dbo.Orders<BR>WHERE OrderDate&lt;@enddate<BR>Order By OrderDate<BR>GO<\/P><br \/>\n<P>The reason is that the default in storedproc must be a constant or NULL<\/P><br \/>\n<P>2) Open the table in Design mode in enterprise manager(Or much better, use ALTER TABLE in Query Analyzer), find the column, and in the &#8220;Default Value&#8221; enter (GETDATE()).Now you won&#8217;t have to worry about populating that column in any procedures Because SQL Server will put in the current date and time when the record is<BR>inserted.<\/P><br \/>\n<P>TIMESTAMP is an unfortunate misnomer, as the data stored here really has nothing to do with TIME at all.&nbsp; ROWVERSION is an equivalent data type and is the syntax I recommend when using to track changes to a row, if for nothing else, to avoid this very ambiguity.&nbsp; Also, I heard that in some future version of SQL Server, the TIMESTAMP designation will go away.You can see these articles:<BR><A href=\"http:\/\/www.aspfaq.com\/2448\">http:\/\/www.aspfaq.com\/2448<\/A><BR><A href=\"http:\/\/www.aspfaq.com\/2499\">http:\/\/www.aspfaq.com\/2499<\/A><\/P><br \/>\n<P>Having said this ,I thought that when do people ever use timestamp &#8211; I mean, why was it ever included. I understand it to be an 8 byte binary number that changes when a record changes, and is unique within the scope of a database, but I don&#8217;t see the benifits of using it.&nbsp; It doesn&#8217;t provide an audit trail of row data as far as I know, and is not a candidate for PK as it changes over time.What benifits are there for using it?. I put my newbie hat on and I did some goole searches \ud83d\ude09<\/P><br \/>\n<P>Answer:<\/P><br \/>\n<P>Optimistic concurrency ( Simple ,isn\u2019t it? :-)).When you read a row to present to a user, read and store the value in the timestamp column, When you later issue the UPDATE:<\/P><br \/>\n<P>UPDATE tbl<BR>SET &#8230;<BR>WHERE pkCol = Your_Primary_Key_Value<BR>AND tsCol = OriginalTimeStampValue<\/P><br \/>\n<P>Then check number of rows modified by the UPDATE. If 0, the row was either deleted or changed or updated by somebody else while you were looking at it.<BR>&nbsp;<BR><\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Couple years ago I was struggling a bit with timestamp (Also known as ROWVERSION) and DateTime data types in Sql Server as I wanted to have the date and time of when each record in Table is modified in my application. Yesterday I saw couple of posts in the newsgroups in this regards so I [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-1102","post","type-post","status-publish","format-standard","hentry"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.4 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>TimeStamp VS DateTime data types in SQL SERVER - Reza Alirezaei&#039;s Blog %<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/blogs.devhorizon.com\/reza\/2005\/07\/30\/timestamp-vs-datetime-data-types-in-sql-server\/\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Reza Alirezaei\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/blogs.devhorizon.com\\\/reza\\\/2005\\\/07\\\/30\\\/timestamp-vs-datetime-data-types-in-sql-server\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/blogs.devhorizon.com\\\/reza\\\/2005\\\/07\\\/30\\\/timestamp-vs-datetime-data-types-in-sql-server\\\/\"},\"author\":{\"name\":\"Reza Alirezaei\",\"@id\":\"https:\\\/\\\/blogs.devhorizon.com\\\/reza\\\/#\\\/schema\\\/person\\\/cdbb24d283697a65951cb4a14e474938\"},\"headline\":\"TimeStamp VS DateTime data types in SQL SERVER\",\"datePublished\":\"2005-07-30T09:07:00+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/blogs.devhorizon.com\\\/reza\\\/2005\\\/07\\\/30\\\/timestamp-vs-datetime-data-types-in-sql-server\\\/\"},\"wordCount\":459,\"commentCount\":0,\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/blogs.devhorizon.com\\\/reza\\\/2005\\\/07\\\/30\\\/timestamp-vs-datetime-data-types-in-sql-server\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/blogs.devhorizon.com\\\/reza\\\/2005\\\/07\\\/30\\\/timestamp-vs-datetime-data-types-in-sql-server\\\/\",\"url\":\"https:\\\/\\\/blogs.devhorizon.com\\\/reza\\\/2005\\\/07\\\/30\\\/timestamp-vs-datetime-data-types-in-sql-server\\\/\",\"name\":\"TimeStamp VS DateTime data types in SQL SERVER - Reza Alirezaei's Blog %\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/blogs.devhorizon.com\\\/reza\\\/#website\"},\"datePublished\":\"2005-07-30T09:07:00+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/blogs.devhorizon.com\\\/reza\\\/#\\\/schema\\\/person\\\/cdbb24d283697a65951cb4a14e474938\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/blogs.devhorizon.com\\\/reza\\\/2005\\\/07\\\/30\\\/timestamp-vs-datetime-data-types-in-sql-server\\\/\"]}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/blogs.devhorizon.com\\\/reza\\\/#website\",\"url\":\"https:\\\/\\\/blogs.devhorizon.com\\\/reza\\\/\",\"name\":\"Reza Alirezaei's Blog\",\"description\":\"Blogging from the field!\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/blogs.devhorizon.com\\\/reza\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/blogs.devhorizon.com\\\/reza\\\/#\\\/schema\\\/person\\\/cdbb24d283697a65951cb4a14e474938\",\"name\":\"Reza Alirezaei\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/3ba940d84e0ecb909e62e93df4c56daf0395c7e53c914467ab2ee73124a7d7b6?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/3ba940d84e0ecb909e62e93df4c56daf0395c7e53c914467ab2ee73124a7d7b6?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/3ba940d84e0ecb909e62e93df4c56daf0395c7e53c914467ab2ee73124a7d7b6?s=96&d=mm&r=g\",\"caption\":\"Reza Alirezaei\"},\"url\":\"https:\\\/\\\/blogs.devhorizon.com\\\/reza\\\/author\\\/rezaa\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"TimeStamp VS DateTime data types in SQL SERVER - Reza Alirezaei's Blog %","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/blogs.devhorizon.com\/reza\/2005\/07\/30\/timestamp-vs-datetime-data-types-in-sql-server\/","twitter_misc":{"Written by":"Reza Alirezaei","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/blogs.devhorizon.com\/reza\/2005\/07\/30\/timestamp-vs-datetime-data-types-in-sql-server\/#article","isPartOf":{"@id":"https:\/\/blogs.devhorizon.com\/reza\/2005\/07\/30\/timestamp-vs-datetime-data-types-in-sql-server\/"},"author":{"name":"Reza Alirezaei","@id":"https:\/\/blogs.devhorizon.com\/reza\/#\/schema\/person\/cdbb24d283697a65951cb4a14e474938"},"headline":"TimeStamp VS DateTime data types in SQL SERVER","datePublished":"2005-07-30T09:07:00+00:00","mainEntityOfPage":{"@id":"https:\/\/blogs.devhorizon.com\/reza\/2005\/07\/30\/timestamp-vs-datetime-data-types-in-sql-server\/"},"wordCount":459,"commentCount":0,"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/blogs.devhorizon.com\/reza\/2005\/07\/30\/timestamp-vs-datetime-data-types-in-sql-server\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/blogs.devhorizon.com\/reza\/2005\/07\/30\/timestamp-vs-datetime-data-types-in-sql-server\/","url":"https:\/\/blogs.devhorizon.com\/reza\/2005\/07\/30\/timestamp-vs-datetime-data-types-in-sql-server\/","name":"TimeStamp VS DateTime data types in SQL SERVER - Reza Alirezaei's Blog %","isPartOf":{"@id":"https:\/\/blogs.devhorizon.com\/reza\/#website"},"datePublished":"2005-07-30T09:07:00+00:00","author":{"@id":"https:\/\/blogs.devhorizon.com\/reza\/#\/schema\/person\/cdbb24d283697a65951cb4a14e474938"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/blogs.devhorizon.com\/reza\/2005\/07\/30\/timestamp-vs-datetime-data-types-in-sql-server\/"]}]},{"@type":"WebSite","@id":"https:\/\/blogs.devhorizon.com\/reza\/#website","url":"https:\/\/blogs.devhorizon.com\/reza\/","name":"Reza Alirezaei's Blog","description":"Blogging from the field!","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/blogs.devhorizon.com\/reza\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/blogs.devhorizon.com\/reza\/#\/schema\/person\/cdbb24d283697a65951cb4a14e474938","name":"Reza Alirezaei","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/3ba940d84e0ecb909e62e93df4c56daf0395c7e53c914467ab2ee73124a7d7b6?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/3ba940d84e0ecb909e62e93df4c56daf0395c7e53c914467ab2ee73124a7d7b6?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/3ba940d84e0ecb909e62e93df4c56daf0395c7e53c914467ab2ee73124a7d7b6?s=96&d=mm&r=g","caption":"Reza Alirezaei"},"url":"https:\/\/blogs.devhorizon.com\/reza\/author\/rezaa\/"}]}},"_links":{"self":[{"href":"https:\/\/blogs.devhorizon.com\/reza\/wp-json\/wp\/v2\/posts\/1102","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blogs.devhorizon.com\/reza\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.devhorizon.com\/reza\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.devhorizon.com\/reza\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.devhorizon.com\/reza\/wp-json\/wp\/v2\/comments?post=1102"}],"version-history":[{"count":0,"href":"https:\/\/blogs.devhorizon.com\/reza\/wp-json\/wp\/v2\/posts\/1102\/revisions"}],"wp:attachment":[{"href":"https:\/\/blogs.devhorizon.com\/reza\/wp-json\/wp\/v2\/media?parent=1102"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.devhorizon.com\/reza\/wp-json\/wp\/v2\/categories?post=1102"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.devhorizon.com\/reza\/wp-json\/wp\/v2\/tags?post=1102"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}