Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

NEWID values to determine newest record 3

Status
Not open for further replies.

AlbertKim247

Programmer
Sep 27, 2006
5
US
My question has to do with identifying the newest record in a dataset that uses the NEWID function to assign a value for the primary key field.

I need to determine the newer of two records that were both created on a particular date.

For example, in a recordset of 2 records created on the same date, could I use the max value of the NEWID value to determine which of the records was created last?

Sample dataset:

Header: KeyField|Name|DateCreated
Record1: 77E08446-01EF-4BEE-BBAB-6ED460D471BB|John Smith|1/1/2007
Record2: D9BB5E77-D9DA-4D40-956F-F16C0FBAB206|John Smith|1/1/2007

Record1 and Record2 were both created on the same date and have the same exact values, but I need to know which one was created last. If I group on all the fields, and take the max value in the KeyField, I would get one record. Would the resulting record be the one that was created last?

Assumptions:
1. From what I understand, the NEWID function creates unique values by taking the mac address of the computer it is created on, and a datetime stamp.
 
There is no way use the value of newid() to find the newest.

As a sample run the following.
Code:
set nocount on
select newid()
select newid()
select newid()

When I just ran it I get
------------------------------------
D723C0C1-F7DB-42D9-BA9D-61EA2C69B7AF


------------------------------------
B285615B-6EDB-498E-8A1F-B4F21DA8B68F


------------------------------------
D46E03A7-0502-40C9-BA5A-1957BFF2395D
As you can see there isn't really any pattern to the first digits or last digits.

If you need the time that the records are added in the table I would recommend adding a column which stores the output from getdate() to hold the date and time the record was added.

Denny
MCSA (2003) / MCDBA (SQL 2000)
MCTS (SQL 2005 / Microsoft Windows SharePoint Services 3.0: Configuration / Microsoft Office SharePoint Server 2007: Configuration)
MCITP Database Administrator (SQL 2005) / Database Developer (SQL 2005)

--Anything is possible. All it takes is a little research. (Me)
[noevil]
 
I totaly agree that you should be using another way to determine the last record. But, for acedemic purposes you might be able to make use of NEWSEQUENTIALID() if you are using SQL 2005.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top