How to use timestamp?

Last post 07-05-2009 11:42 PM by RickNZ. 4 replies.

Sort Posts:

  • How to use timestamp?

    07-05-2009, 1:46 AM
    • Member
      95 point Member
    • aspquestion
    • Member since 04-29-2009, 12:05 PM
    • Posts 137

    Hello

    I have a stored procedure that will insert data to table like this:

    CREATE PROCEDURE dataAdd

                (

                 @1stName text,

                 @lastName text

                )

     AS

                INSERT INTO namesTable

                            (1stName,lastName)

                            VALUES

                            (@1stName, @lastName)

    GO

    I want to be able to retrieve the names ordered by listing time.

    I want to use timestamp and I never used it before.

    How can I do this?

     

    Thank you

  • Re: How to use timestamp?

    07-05-2009, 7:34 AM
    Answer

    instead of using timestamp column take another column with datatype datetime and set its default value as "getdate()". using timesetamp  column is difficult to compare as it will store in binary format..

    Give a man a fish and you feed him for a day. Teach a man to fish and you feed him forever.
  • Re: How to use timestamp?

    07-05-2009, 1:20 PM
    Answer
    • Contributor
      5,228 point Contributor
    • RickNZ
    • Member since 01-01-2009, 3:43 AM
    • Nelson, New Zealand
    • Posts 873

    The term timestamp has been deprecated.

    To use this feature, declare a column in your table with type rowversion.  Whenever the row is modified, the value of that column will be changed.  You can use such a column for concurrency control.  You can also use it in sorts or range checks.

    However, for a pure date-oriented function as you describe, a datetime field is probably a better idea.

  • Re: How to use timestamp?

    07-05-2009, 7:36 PM
    Answer
    • Star
      12,723 point Star
    • malcolms
    • Member since 06-12-2008, 4:38 AM
    • Melbourne, Australia
    • Posts 2,095

    Add a DateCreated field that has a datetime data type.  Then when you insert the record, use GETDATE() to add the current date:

    INSERT INTO namesTable

                            (1stName,lastName, datecreated)

                            VALUES

                            (@1stName, @lastName, GETDATE())

    Then in your select, order by datecreated

    select * from namesTable order by datecreated

    Sincerely,
    Malcolm Sheridan

    Microsoft Certified Solution Developer
    Please remember to click "Mark as Answer" on the post that helps you, and to click "Unmark as
    Answer" if a marked post does not actually answer your question.
  • Re: How to use timestamp?

    07-05-2009, 11:42 PM
    Answer
    • Contributor
      5,228 point Contributor
    • RickNZ
    • Member since 01-01-2009, 3:43 AM
    • Nelson, New Zealand
    • Posts 873

    If you want to track insert dates, you could also do this:

    create table namesTable (
        Id int identity primary key,
        FirstName varchar(128),
        LastName varchar(128),
        InsertDate datetime default (getutcdate())
    )
    

    The difference between that and rowversion is that rowversion also tracks when you make changes to any column in the row.

Page 1 of 1 (5 items)