SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Member
    Join Date
    Jan 2005
    Location
    north carolina
    Posts
    1
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    DATEDIFF function problems

    I tried recently on mssql server the fuction datediff in a stored procedure. No matter what I try when I do a datediff on two datetime times, I get Jan 1 1900 12:00 AM.

    this is the code:

    assume all variables are declared


    SET @diff = SELECT(DATEDIFF(day, GETDATE(), GETDATE()))
    PRINT CAST(@diff as varchar)

    perhaps its the cast that makes it print this time. If so how can I print the correct value? what is the datetype returned by this function datediff? is it not a datetime datatype? thank you

  2. #2
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,251
    Mentioned
    59 Post(s)
    Tagged
    3 Thread(s)
    the difference in days between two dates is an integer

    in your example, the difference in days between getdate() and getdate() is 0
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"

  3. #3
    SitePoint Addict danfran's Avatar
    Join Date
    Jan 2005
    Location
    New York City
    Posts
    244
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I think the point that really needs to be clarified is: getdate() returns the instant date and time, right as it's called.

    the amount of days between those two getdate() calls is on the order of nano-seconds! -not too very many days.. -essentially "zero".

    try this: select datediff(day,'nov 22 1963', getdate())

  4. #4
    SitePoint Guru asterix's Avatar
    Join Date
    Jun 2003
    Posts
    847
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by xrecovx
    SET @diff = SELECT(DATEDIFF(day, GETDATE(), GETDATE()))
    PRINT CAST(@diff as varchar)
    Try this

    Code:
    declare @diff int
     SELECT @diff =(DATEDIFF(day, GETDATE(), GETDATE()))
     PRINT @diff
    or even this:

    Code:
    PRINT (DATEDIFF(day, GETDATE(), GETDATE()))
    HTH


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •