|
Home > Archive > Microsoft SQL Server forum > September 2005 > Finding the most recient datetime
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
Finding the most recient datetime
|
|
| JohnnyRuin 2005-09-26, 1:24 pm |
|
Hi,
My table has int Field1 and DateTime dtModificationTime. I maintain a
history of changes to the table by inserting a new record with a new
dtModificationTime. I need to be able to find the last inserted
record. It's easy to do Select * from table where field1=x ORDER BY
dtModificationTime. That works, but it hits all the records that
match the field. I need to come back with an exact hit. Can someone
point me in the right direction?
Thanks.
| |
| manrajshekar@yahoo.com 2005-09-26, 1:24 pm |
| Hi Johnny,
Use this
select top 1 * from table where field = X order by update_dt desc.
This would solve your problem.
Regards,
-Manoj Rajshekar
| |
| Simon Hayes 2005-09-26, 1:24 pm |
| JohnnyRuin wrote:
> Hi,
>
> My table has int Field1 and DateTime dtModificationTime. I maintain a
> history of changes to the table by inserting a new record with a new
> dtModificationTime. I need to be able to find the last inserted
> record. It's easy to do Select * from table where field1=x ORDER BY
> dtModificationTime. That works, but it hits all the records that
> match the field. I need to come back with an exact hit. Can someone
> point me in the right direction?
>
> Thanks.
>
See "Limiting Result Sets Using TOP and PERCENT", SET ROWCOUNT and
SELECT in Books Online:
select top 1 Field1, dtModificationTime
from dbo.MyTable
order by dtModificationTime desc
You should probably also consider what happens when you have multiple
rows with the same value in dtModificationTime.
Simon
| |
| Jerry Spivey 2005-09-26, 8:24 pm |
| Use the MAX function.
HTH
Jerry
"JohnnyRuin" <schafer.dave@gmail.com> wrote in message
news:1127754721.101989.205780@f14g2000cwb.googlegroups.com...
>
> Hi,
>
> My table has int Field1 and DateTime dtModificationTime. I maintain a
> history of changes to the table by inserting a new record with a new
> dtModificationTime. I need to be able to find the last inserted
> record. It's easy to do Select * from table where field1=x ORDER BY
> dtModificationTime. That works, but it hits all the records that
> match the field. I need to come back with an exact hit. Can someone
> point me in the right direction?
>
> Thanks.
>
|
|
|
|
|