Finding the most recient datetime

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • JohnnyRuin

    Finding the most recient datetime


    Hi,

    My table has int Field1 and DateTime dtModificationT ime. I maintain a
    history of changes to the table by inserting a new record with a new
    dtModificationT ime. I need to be able to find the last inserted
    record. It's easy to do Select * from table where field1=x ORDER BY
    dtModificationT ime. 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

    #2
    Re: Finding the most recient datetime

    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

    Comment

    • Simon Hayes

      #3
      Re: Finding the most recient datetime

      JohnnyRuin wrote:[color=blue]
      > Hi,
      >
      > My table has int Field1 and DateTime dtModificationT ime. I maintain a
      > history of changes to the table by inserting a new record with a new
      > dtModificationT ime. I need to be able to find the last inserted
      > record. It's easy to do Select * from table where field1=x ORDER BY
      > dtModificationT ime. 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.
      >[/color]
      See "Limiting Result Sets Using TOP and PERCENT", SET ROWCOUNT and
      SELECT in Books Online:

      select top 1 Field1, dtModificationT ime
      from dbo.MyTable
      order by dtModificationT ime desc

      You should probably also consider what happens when you have multiple
      rows with the same value in dtModificationT ime.

      Simon

      Comment

      • Jerry Spivey

        #4
        Re: Finding the most recient datetime

        Use the MAX function.

        HTH

        Jerry
        "JohnnyRuin " <schafer.dave@g mail.com> wrote in message
        news:1127754721 .101989.205780@ f14g2000cwb.goo glegroups.com.. .[color=blue]
        >
        > Hi,
        >
        > My table has int Field1 and DateTime dtModificationT ime. I maintain a
        > history of changes to the table by inserting a new record with a new
        > dtModificationT ime. I need to be able to find the last inserted
        > record. It's easy to do Select * from table where field1=x ORDER BY
        > dtModificationT ime. 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.
        >[/color]


        Comment

        Working...