MS Access Date() Function and SQL Server Query

user1457613

I cant seem to find a straight answer to what seems is common scenario. I recently converted an access db to SQL Server, and have linked tables (odbc). Since dates are now being stored as in datetime2 as yyyy-mm-dd hh:mm:ss, i have had no problem in VBA to have my forms write to the database like Format(VBA.Date, "yyyy-mm-dd 00:00:00").

However I have a few simple queries in Access that had a where clause like:

Where Field = Date()

and also

Where Field Between Date() And Date()-7

What is the proper way to query these from an Access query to SQL server?

M.Ali

Your Access Code

Where Field = Date()

Sql Server

Where Field = CAST(GETDATE() AS DATE)

Function GETDATE() gets the current Datetime.

Date between today and last 7 days

Where Field BETWEEN CAST(DATEADD(DAY, -7, GETDATE()) AS DATE) 
                     AND CAST(GETDATE() AS DATE)

The reason I have used CAST() function is because GETDATE() returns the current Datetime something like this 2014-02-09 22:09:53.067

now if you are checking values WHERE Field has the today's date it will only the the values where Field = '2014-02-09 22:09:53.067' Not the records where Date 2014-02-09 it will also take the time into consideration there for I used the CAST Function to get rid of the time part from GETDATE() function.

SELECT GETDATE()  Returns '2014-02-09 22:09:53.067'

SELECT CAST(GETDATE() AS DATE) Returns '2014-02-09'

I have fixed the issues with your query there were quite a few of them :S I would suggest reading books online and learn the correct synatx for sql server , see below the fixed query

SELECT TransactionTotals.[Date]
    , TransactionTotals.EntryID
    , TransactionItems.ItemID
    , TransactionItems.ClientID
    , [FirstName] + ' ' + [LastName] AS Name
    , TransactionItems.[Service]
    , TransactionItems.Therapist
    , TransactionItems.GiftCertificate
    , TransactionItems.Charge
    , TransactionItems.Gratuity
    , TransactionTotals.Paid
    , TransactionItems.Comments 

FROM   CLIENTS INNER JOIN TransactionItems
ON     CLIENTS.ClientID = TransactionItems.ClientID 
INNER JOIN TransactionTotals
ON     TransactionTotals.EntryID = TransactionItems.EntryID
WHERE  TransactionTotals.[Date] = CAST(GETDATE() AS DATE) 
ORDER BY TransactionTotals.EntryID DESC;

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

MS Access SQL Insert Query

From Dev

MS Access Date() Function and SQL Server Query

From Dev

MS Access SQL Server DB - Query Syntax for CAST Function

From Dev

In MS Access SQL Server Linked Table pass through query

From Dev

Convert MS Access data query into SQL Server database query

From Dev

How to add a dropdown to datasheet form in MS Access which is based off a SQL Server query

From Dev

Converting a function from SQL Server to MS Access

From Dev

Convert MS Access count and pivot query into SQL Server

From Dev

Ms-Access Open a Query Sourced from SQL Server stored procedure

From Dev

SQL Query tuning - MS SQL Server -2012

From Dev

Convert SQl query to MS Access

From Dev

Access SQL Like * alternative in MS SQL server

From Dev

Using ODBC in Access to connect to MS SQL Server 2012: huge time difference between calling query manually and in VBA

From Dev

MS SQL Server Query Date functions to MySQL

From Dev

Create stored procedure in SQL Server from a MS Access update query

From Dev

MS Access Database SQL Query

From Dev

MS Access SQL Server DB - Query Syntax for CAST Function

From Dev

SQL Server Query in Access

From Dev

SQL Query PIVOT to MS Access SQL Query

From Dev

Convert MS Access data query into SQL Server database query

From Dev

MS Access SQL Query does not include specified expression as aggregated function

From Dev

MS Access Date not working in VBA SQL query

From Dev

MS Access SQL Query statement

From Dev

Access SQL Like * alternative in MS SQL server

From Dev

Using ODBC in Access to connect to MS SQL Server 2012: huge time difference between calling query manually and in VBA

From Dev

SQL Server LEAD Equivalent Function in MS Access 2016

From Dev

MS SQL Server QUERY SUM of each item between start date and end date

From Dev

MS Access SQL Date Range Query

From Dev

SQL Server 2008 R2: Query MS Access from SQL Server

Related Related

  1. 1

    MS Access SQL Insert Query

  2. 2

    MS Access Date() Function and SQL Server Query

  3. 3

    MS Access SQL Server DB - Query Syntax for CAST Function

  4. 4

    In MS Access SQL Server Linked Table pass through query

  5. 5

    Convert MS Access data query into SQL Server database query

  6. 6

    How to add a dropdown to datasheet form in MS Access which is based off a SQL Server query

  7. 7

    Converting a function from SQL Server to MS Access

  8. 8

    Convert MS Access count and pivot query into SQL Server

  9. 9

    Ms-Access Open a Query Sourced from SQL Server stored procedure

  10. 10

    SQL Query tuning - MS SQL Server -2012

  11. 11

    Convert SQl query to MS Access

  12. 12

    Access SQL Like * alternative in MS SQL server

  13. 13

    Using ODBC in Access to connect to MS SQL Server 2012: huge time difference between calling query manually and in VBA

  14. 14

    MS SQL Server Query Date functions to MySQL

  15. 15

    Create stored procedure in SQL Server from a MS Access update query

  16. 16

    MS Access Database SQL Query

  17. 17

    MS Access SQL Server DB - Query Syntax for CAST Function

  18. 18

    SQL Server Query in Access

  19. 19

    SQL Query PIVOT to MS Access SQL Query

  20. 20

    Convert MS Access data query into SQL Server database query

  21. 21

    MS Access SQL Query does not include specified expression as aggregated function

  22. 22

    MS Access Date not working in VBA SQL query

  23. 23

    MS Access SQL Query statement

  24. 24

    Access SQL Like * alternative in MS SQL server

  25. 25

    Using ODBC in Access to connect to MS SQL Server 2012: huge time difference between calling query manually and in VBA

  26. 26

    SQL Server LEAD Equivalent Function in MS Access 2016

  27. 27

    MS SQL Server QUERY SUM of each item between start date and end date

  28. 28

    MS Access SQL Date Range Query

  29. 29

    SQL Server 2008 R2: Query MS Access from SQL Server

HotTag

Archive