Results 1 to 6 of 6

Thread: DateDiff and SQL sort

  1. #1

    Thread Starter
    Fanatic Member x-ice's Avatar
    Join Date
    Mar 2004
    Location
    UK
    Posts
    671

    Resolved DateDiff and SQL sort

    Firstly i want to use DateDiff to establish the amount of time between a Login and Logout time? Can this be done? and what values can DateDiff return?

    Also, how can i sort by Date using if i'm using an SQL statement? When i try to do this only the first segment of the date is sorted, but everything after the first slash isn't sorted. How can i sort using the whole date?

    thanks
    Last edited by x-ice; Feb 26th, 2005 at 08:01 PM.

  2. #2
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: DateDiff and SQL sort

    DateDiff(interval,Date1,Date2[,FirstDayofWeek[,FirstWeekOfYear]

    Interval can be:
    yyyy Year
    q Quarter
    m Month
    y Day of Year
    d Day
    w Weekday
    ww Week
    h Hour
    m Minute
    s Second

    FirstDayofWeek 0 - System Date
    1 - Sunday
    7 - Saturday

    FirstWeekofYear 0 - System
    1 Week when Jan 1 occurs
    2 Has at least 4 days in it
    3 First Full Week of year

    Put the Interval in Quotes.

    VB Code:
    1. DateDiff("d",now,"2/20/05")  = 1

    Second question:
    VB Code:
    1. "SELECT * FROM tablename ORDER BY field2 DESC"

  3. #3
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: DateDiff and SQL sort

    The sorting issue may be from the field being a data type other then a Date/Time format.
    So when you have, say a text field storing string date values, it will sort based on text
    values and not date values.

    If you need to retain a text field data type then you can format the field in whatever control
    you presenting it in with a preceeding zero for single digit months and also for the days and
    years, depending on how you want the format to be.

    1/1/01 <-> 01/01/2005 This will sort correctly in either data or text formats.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  4. #4

    Thread Starter
    Fanatic Member x-ice's Avatar
    Join Date
    Mar 2004
    Location
    UK
    Posts
    671

    Re: DateDiff and SQL sort

    Can DateDiff only return whole numbers?

    i did
    Code:
    MsgBox DateDiff("h", "13:00:00", "16:30:00")
    and it returned 3 instead of 3:30

    p.s. the data type of Date is Date/Time and i know how to construct the SQL statement, but it doesn't work as explained earlier

  5. #5
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: DateDiff and SQL sort

    You could use Minutes instead

  6. #6

    Thread Starter
    Fanatic Member x-ice's Avatar
    Join Date
    Mar 2004
    Location
    UK
    Posts
    671

    Re: DateDiff and SQL sort

    I then get a return of 0

Posting Permissions

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



Click Here to Expand Forum to Full Width