Results 1 to 8 of 8

Thread: SQL Date Help

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Nov 2004
    Posts
    144

    SQL Date Help

    Hey,

    I'm creating a service program in Access 2003

    I have two tables:

    tbl_Appliance
    ApplianceNo (Primary Key)
    SerialNo
    ApplianceType
    ApplianceManufacturer
    ApplianceModel
    ApplianceDescription

    tbl_NextService
    Nxt_Service_ID (Primary Key)
    Appliance_ID (Foreign Key)
    Nxt_Service_Date

    as you can see the tbl_Appliance stores all data about the appliance, the tbl_NextService table stores the next service date of that appliance.

    What i need to do is find all the dates stored within the database that match the month of the Computers system date. is this possible using access?

    e.g.

    if i have the date 23/03/2005 within the tbl_NextService table, this date will be queried as the system date is the 14/03/2005 (Date is in the same month as the system date)

    VB Code:
    1. SELECT tbl_Appliance.*, tbl_NextService.Nxt_Service_Date
    2. FROM tbl_Appliance INNER JOIN tbl_NextService ON tbl_Appliance.Appliance_No = tbl_NextService.Appliance_ID

    how would i add a WHERE clause to the above SQL statement

    Thanks

  2. #2
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: SQL Date Help

    Code:
    SELECT tbl_Appliance.*, tbl_NextService.Nxt_Service_Date
    FROM tbl_Appliance INNER JOIN tbl_NextService ON tbl_Appliance.Appliance_No = tbl_NextService.Appliance_ID
    WHERE MONTH(tbl_NextService.Nxt_Service_Date) = MONTH(NOW)
      AND YEAR(tbl_NextService.Nxt_Service_Date) = YEAR(NOW)
    Think that will do it.....if you are using Access. Otherwise, if you are using SQL Server, replace NOW with GETDATE()

    Tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Nov 2004
    Posts
    144

    Re: SQL Date Help

    Hey techgnome,

    works great thanks

    Cheers

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Nov 2004
    Posts
    144

    Re: SQL Date Help

    HEY again

    thanks again for the SQL

    whould it also be possible to have the SQL WHERE clause you gave me to work to find dates that are in that WEEK thanks

  5. #5
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: SQL Date Help

    Hmmm.....I've never tried that, but if there is a WEEK function, then it would be the same, except replace MONTH with WEEK.... But that's if it exists. IF it doesn't then you may need to look at the Format function and see if there is an option in there that returns the week number.

    Tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    Nov 2004
    Posts
    144

    Re: SQL Date Help

    Tired the WEEK function but seems not to exist..oh well....

    I have another question for you.

    How would i query the current system date to data in the tables

    e.g

    If the date today is the 15/03/2005 then all the dates 15/03/2005 in the table will be quiried...any idea how i would do tha?

    i think i could use the Date() function but not sure how to set the SQL

    Any help would be great

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    Nov 2004
    Posts
    144

    Re: SQL Date Help

    Hey

    i have a solution to finding dates that is between the system date and the next seven days anyway. (Technically a week...lol)

    VB Code:
    1. SELECT tbl_Appliance.*, tbl_NextService.Nxt_Service_Date
    2. FROM tbl_Appliance INNER JOIN tbl_NextService ON tbl_Appliance.Appliance_No = tbl_NextService.Appliance_ID
    3. WHERE (((tbl_NextService.Nxt_Service_Date) Between Date() And Date()+7));

    The above SQL finds dates that are between the current date and the date in seven days time...

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

    Re: SQL Date Help

    Since you are only asking for the date and not the time then that will work fine. Another syntax is
    the SomeDate >= SomeOtherDate And SomeDate < SomeEndDate + 1.

    When you think about it, its not that complicated as some people make it out to be.
    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

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