2

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?

1

1 Answer 1

2

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;
Sign up to request clarification or add additional context in comments.

10 Comments

WHERE TransactionTotals.Date=CAST(GETDATE() AS DATE) gets 'Syntax Error (missing operator)
@user1457613 Post the full query in your question.
Your this bit of query is correct there is something else somewhere else you are missing
Well, its the only line being changed but:
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 (TransactionTotals INNER JOIN TransactionItems ON TransactionTotals.EntryID = TransactionItems.EntryID) ON CLIENTS.ClientID = TransactionItems.ClientID
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.