dbTalk Databases Forums  

Finding Last Updated with a timestamp

comp.database.ms-sqlserver comp.database.ms-sqlserver


Discuss Finding Last Updated with a timestamp in the comp.database.ms-sqlserver forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Astra
 
Posts: n/a

Default Finding Last Updated with a timestamp - 07-16-2004 , 06:05 AM






Hi All

I know an SQL Server timestamp seems to be as useful as rocking horse for
show jumping, but I'm hoping you know a 'fudge' to get me round a problem or
at least confirm that it isn't possible.

I have 2 tables, one called ACCOUNTS and one called STOCK.

These tables have the usual ints, varchars, etc and have a timestamp field
as well.

My end user wants to see a simple list of the details in these tables
(individually - no joins present here), but sorted from most recently
updated to never touched.

As the timestamp seems to update each time a transaction hits it I though
this would be perfect, but I've had the following mixed results:

1) I did a simple ORDER BY TIMESTAMP DESC, but because the order is
alphanumeric I don't get a true 'recent to old' list.

2) So I did ORDER BY CAST(TIMESTAMP AS INT) DESC. There is a numeric sort
now, but the timestamp values seem to be hit and miss so that an account
that I know should be near the top is around about the middle.

Do you know how I can achieve the results I want?

Is the timestamp a waste of time?

Thanks

Robbie




Reply With Quote
  #2  
Old   
Aaron [SQL Server MVP]
 
Posts: n/a

Default Re: Finding Last Updated with a timestamp - 07-16-2004 , 06:14 AM






Quote:
Do you know how I can achieve the results I want?
Add a DATETIME column and update this whenever the row is updated.
http://www.aspfaq.com/2499
http://www.aspfaq.com/2448
http://www.aspfaq.com/2496

Quote:
Is the timestamp a waste of time?
For what you're trying to accomplish, yes. Timestamp has absolutely nothing
to do with date and time.

--
http://www.aspfaq.com/
(Reverse address to reply.)




Reply With Quote
  #3  
Old   
Astra
 
Posts: n/a

Default Re: Finding Last Updated with a timestamp - 07-16-2004 , 06:46 AM



Hi Aaron

Thanks for the prompt reply.

Yes, I know the timestamp isn't a date or time thing, but I didn't know
whether say the integer value of this timestamp is actually an ever
increasing value so that the higher the number the more up-to-date it is.
Something along these lines, purely so that I can sort them by some method
to get the latest to the top of the list.

Any ideas?

Rgds

Robbie/Laphan

"Aaron [SQL Server MVP]" <ten.xoc (AT) dnartreb (DOT) noraa> wrote

Quote:
Do you know how I can achieve the results I want?
Add a DATETIME column and update this whenever the row is updated.
http://www.aspfaq.com/2499
http://www.aspfaq.com/2448
http://www.aspfaq.com/2496

Quote:
Is the timestamp a waste of time?
For what you're trying to accomplish, yes. Timestamp has absolutely nothing
to do with date and time.

--
http://www.aspfaq.com/
(Reverse address to reply.)





Reply With Quote
  #4  
Old   
Aaron [SQL Server MVP]
 
Posts: n/a

Default Re: Finding Last Updated with a timestamp - 07-16-2004 , 07:50 AM



Quote:
Yes, I know the timestamp isn't a date or time thing, but I didn't know
whether say the integer value of this timestamp is actually an ever
increasing value so that the higher the number the more up-to-date it is.
Something along these lines, purely so that I can sort them by some method
to get the latest to the top of the list.
If I ORDER BY [TIMESTAMP_COLUMN] DESC (without converting it to an int or
varchar), it seems to show me the rows in the order they were
inserted/updated. However if you want to rely on it, please consider what I
posted earlier: use a DATETIME or SMALLDATETIME column.

--
http://www.aspfaq.com/
(Reverse address to reply.)




Reply With Quote
  #5  
Old   
John Bell
 
Posts: n/a

Default Re: Finding Last Updated with a timestamp - 07-16-2004 , 10:46 AM



Hi

Timestamp is not a character field.. as stated in books online:

A nonnullable timestamp column is semantically equivalent to a binary(8)
column. A nullable timestamp column is semantically equivalent to a
varbinary(8) column.

When you select the column in QA, it will be displayed as a hexadecimal
number.

If you include DDL ( Create table statements etc... ) and example data (as
Insert statements) along with your query and the output, it may be clearer
what is occuring.

John


"Astra" <info (AT) NoEmail (DOT) com> wrote

Quote:
Hi All

I know an SQL Server timestamp seems to be as useful as rocking horse for
show jumping, but I'm hoping you know a 'fudge' to get me round a problem
or
at least confirm that it isn't possible.

I have 2 tables, one called ACCOUNTS and one called STOCK.

These tables have the usual ints, varchars, etc and have a timestamp field
as well.

My end user wants to see a simple list of the details in these tables
(individually - no joins present here), but sorted from most recently
updated to never touched.

As the timestamp seems to update each time a transaction hits it I though
this would be perfect, but I've had the following mixed results:

1) I did a simple ORDER BY TIMESTAMP DESC, but because the order is
alphanumeric I don't get a true 'recent to old' list.

2) So I did ORDER BY CAST(TIMESTAMP AS INT) DESC. There is a numeric
sort
now, but the timestamp values seem to be hit and miss so that an account
that I know should be near the top is around about the middle.

Do you know how I can achieve the results I want?

Is the timestamp a waste of time?

Thanks

Robbie






Reply With Quote
  #6  
Old   
Erland Sommarskog
 
Posts: n/a

Default Re: Finding Last Updated with a timestamp - 07-17-2004 , 05:34 PM



Astra (info (AT) NoEmail (DOT) com) writes:
Quote:
My end user wants to see a simple list of the details in these tables
(individually - no joins present here), but sorted from most recently
updated to never touched.

As the timestamp seems to update each time a transaction hits it I though
this would be perfect, but I've had the following mixed results:

1) I did a simple ORDER BY TIMESTAMP DESC, but because the order is
alphanumeric I don't get a true 'recent to old' list.

2) So I did ORDER BY CAST(TIMESTAMP AS INT) DESC. There is a numeric
sort now, but the timestamp values seem to be hit and miss so that an
account that I know should be near the top is around about the middle.
Since timestamp is 8 bytes and int only 4, a cast to int could lead to
funny things.

On the other hand, ORDER BY TIMESTAMP DESC will give you the rows in
some order that pertains to updates - and inserts. But it can be a bit
rough. Say that you at some point reloaded the table because of some
maintenance thing, that gave you new timestamp values.

So you are probably better off adding a "moddate" column, which you
update in case of "true" updates, either through stored procedures or a
trigger.


--
Erland Sommarskog, SQL Server MVP, esquel (AT) sommarskog (DOT) se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp


Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.