Date Difference

Discussion in 'Databases' started by cancer10, Jun 18, 2007.

  1. #1
    Hello,

    I have MS Sql server 2000 database and I use ASP as front end.

    I have the following table

    ---------------------
    Name | LastLogin
    --------------------
    ABC | 6/5/2007
    DEF | 9/12/2006
    MNO | 1/15/2007
    XYZ | 6/5/2006
    ------------------


    I want to select the name of persona who have logged in in the past 7 days.

    The following query does not work for some reasons.

    SELECT * from userLogin where DATEDIFF(d, GETDATE(), LastLogin) <= 7


    Thanx for your help.
     
    cancer10, Jun 18, 2007 IP
  2. krt

    krt Well-Known Member

    Messages:
    829
    Likes Received:
    38
    Best Answers:
    0
    Trophy Points:
    120
    #2
    Where did that "d" variable come from?
    DATEDIFF should only have 2 arguments, most probably just GETDATE() and LastLogin.
     
    krt, Jun 18, 2007 IP
  3. cancer10

    cancer10 Guest

    Messages:
    364
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    "d" will show the difference in "days"
     
    cancer10, Jun 18, 2007 IP
  4. krt

    krt Well-Known Member

    Messages:
    829
    Likes Received:
    38
    Best Answers:
    0
    Trophy Points:
    120
    #4
    Great. That made me feel like an idiot :p (didn't think Transact SQL when seeing MSSQL)

    Back to the question:
    Can you show the result of:
    SELECT LastLogin, DATEDIFF(d, LastLogin, GETDATE()) FROM userLogin
    Code (markup):
    And what do you mean "not working?". Is it returning all the results? Maybe because it is date2-date1 so it should be DATEDIFF(d, LastLogin, GETDATE()), not the other way round...
     
    krt, Jun 18, 2007 IP
  5. Clark Kent

    Clark Kent Guest

    Messages:
    122
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    0
    #5
    This is wrong If I am remember correct: DATEDIFF(d, LastLogin, GETDATE())
    Correct for is:
    DATEDIFF("d", LastLogin, GETDATE())
    or
    DATEDIFF(day, LastLogin, GETDATE())

    I prefer second one because of I don't need to use aposthropes (") and don't need to escape.
     
    Clark Kent, Jun 18, 2007 IP
  6. flippers.be

    flippers.be Peon

    Messages:
    432
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #6
    you can always put ABS() around it :)
     
    flippers.be, Jun 20, 2007 IP
  7. cancer10

    cancer10 Guest

    Messages:
    364
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    what is ABS() ?
     
    cancer10, Jun 20, 2007 IP
  8. flippers.be

    flippers.be Peon

    Messages:
    432
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #8
    returns the absolute number, so with + or -

    then it doesn't matter what variable goes first in datediff, either it'll return 7 or -7 and with abs(datedif(..)) both will qualify
     
    flippers.be, Jun 20, 2007 IP
  9. krt

    krt Well-Known Member

    Messages:
    829
    Likes Received:
    38
    Best Answers:
    0
    Trophy Points:
    120
    #9
    It's better to just get it right and not use workarounds. cancer10, did you solve the problem?
     
    krt, Jun 20, 2007 IP