Link to home
Start Free TrialLog in
Avatar of cansevin
cansevin

asked on

Where clause in query

I am trying to use the following query to pull up records of only the current day. For some reason it comes up with no entries... when there should be a couple entries from today.

SELECT qryBookingDayswithYearIII.DayRecorded, qryBookingDayswithYearIII.CustomerID, Avg(qryBookingDayswithYearIII.SumOfExtendedPrice) AS AvgOfSumOfExtendedPrice, ([FirstDay]) AS DateField, "DAY" AS DateSpec
FROM qryBookingDayswithYear INNER JOIN qryBookingDayswithYearIII ON qryBookingDayswithYear.CustomerID = qryBookingDayswithYearIII.CustomerID
WHERE (((qryBookingDayswithYearIII.DayRecorded)=DateValue(Now())))
GROUP BY qryBookingDayswithYearIII.DayRecorded, qryBookingDayswithYearIII.CustomerID, ([FirstDay]), "DAY";

I also used a "WHERE" clause as below. That pulled up a warning "Data type mismatch in criteria expression"
WHERE (((DateValue(Nz([qryBookingDayswithYearIII].[DayRecorded],#1/1/1950#)))=DateValue(Now())))
Avatar of Scott McDaniel (EE MVE )
Scott McDaniel (EE MVE )
Flag of United States of America image

What DataType is [DayRecorded]?
Avatar of cansevin
cansevin

ASKER

At the risk of sounding stupid... how would I find that out? Originally on the table it is Date and Time. It has run through a few queries, not sure if that changes the DataType.
Chances are that [DayRecorded] is stored using Now() rather than Date(), so that it contains a time increment other than 12:00:00 AM (which is the value assigned if you use the Date()) function.

I'm not sure why you would use:

DateValue(Now())

instead of simply using Date()

Have you tried creating a computed column in the query (no where clause) that uses:

TestDate:DateValue(Nz([qryBookingDayswithYearIII].[DayRecorded],#1/1/1950#))

or possibly:

TestDate: cdate(Nz([qryBookingDayswithYearIII].[DayRecorded],#1/1/1950#))
If it has "run through a few queries" and been massaged using either the NZ( ) or Format( ) function, the query will change the data type to a string, so you will have to convert that back to a date.

cDate requires a "date expression" which, according to Access help is:

Any expression that can be interpreted as a date, including date literals, numbers that look like dates, strings that look like dates, and dates returned from functions. A date expression is limited to numbers or strings, in any combination, that can represent a date from January 1, 100 – December 31, 9999.

Dates are stored as part of a real number. Values to the left of the decimal represent the date; values to the right of the decimal represent the time. Negative numbers represent dates prior to December 30, 1899


The DateValue function "argument is normally a string expression representing a date"
That first Test Date does work... it pulls up a column that has the all the dates in it. The TestDate is in the format 1/10/2014 where the DateField I am trying to use is in the format 01-10-14.

Not sure what that means or what I should do with it.

Thanks for helping!
Given that the first "TestDate" works, try a WHERE clause of:

WHERE DateValue(Nz([qryBookingDayswithYearIII].[DayRecorded],#1/1/1950#) = Date()
Warning pops up..

Syntax error (missing operator) in query expression 'DateValue(Nz([qryBookingDayswithYearIII].[DayRecorded],#1/1/1950#) = Date() GROUP BY qryBookingDayswithYeearIII.DayRecorded, qryBookingDayswithYEarIII.CustomerID, ([FirstDay]), "DAY";'.
Looks like you may be missing a ")" to close out the DateValue( ), right before the = in the where clause.

Try:

SELECT qryBookingDayswithYearIII.DayRecorded
, qryBookingDayswithYearIII.CustomerID
, Avg(qryBookingDayswithYearIII.SumOfExtendedPrice) AS AvgOfSumOfExtendedPrice
, [FirstDay] AS DateField
, "DAY" AS DateSpec
FROM qryBookingDayswithYear
INNER JOIN qryBookingDayswithYearIII
ON qryBookingDayswithYear.CustomerID = qryBookingDayswithYearIII.CustomerID
WHERE DateValue(NZ([qryBookingDayswithYearIII].[DayRecorded], #1/1/1950#))=Date()
GROUP BY qryBookingDayswithYearIII.DayRecorded
, qryBookingDayswithYearIII.CustomerID
, [FirstDay]
, "DAY";
Thanks... but now it is back to "Data Type Mismatch in query expression"
remove the GroupBy and see if it works.

What are the datatypes of the CustomerID field in the two queries?  Open each of those queries individually, where does the CustomerID line up (left or right side) of the column?
Yes... there are two customer ID's. Both of them line up to the left.
Have you tried:

SELECT qryBookingDayswithYearIII.DayRecorded
, qryBookingDayswithYearIII.CustomerID
, qryBookingDayswithYearIII.SumOfExtendedPrice
, [FirstDay] AS DateField
, "DAY" AS DateSpec
FROM qryBookingDayswithYear
INNER JOIN qryBookingDayswithYearIII
ON qryBookingDayswithYear.CustomerID = qryBookingDayswithYearIII.CustomerID
WHERE DateValue(NZ([qryBookingDayswithYearIII].[DayRecorded], #1/1/1950#))=Date()

Still don't know what [FirstDay] is? or which of the queries it comes from.  Why doesn't that field have a query identifier?
Ahh man, this sucks!

That comes from the qryBookingDayswithYear.[FirstDay]

It still comes up as data type mismatch. It works fine with out the where, but the where makes it not work.
try it with the cdate conversion function:

WHERE cDate(NZ([qryBookingDayswithYearIII].[DayRecorded], #1/1/1950#))=Date()

As I stated in an earlier post, cdate will accept virtually any format (string, number,date) and it will return a date.  DateValue generally requires a string.

I know, I also said that NZ( ) will always return a string in a query.

Just give it a try.
Unfortunately still the same... below is the query I'm using. I appreciate your help on this. If there is anything else you can think of, let me know.

SELECT qryBookingDayswithYearIII.DayRecorded, qryBookingDayswithYearIII.CustomerID, Avg(qryBookingDayswithYearIII.SumOfExtendedPrice) AS AvgOfSumOfExtendedPrice, qryBookingDayswithYear.[FirstDay] AS DateField, "DAY" AS DateSpec, DateValue(Nz([qryBookingDayswithYearIII].[DayRecorded],#1/1/1950#)) AS TestDate
FROM qryBookingDayswithYear INNER JOIN qryBookingDayswithYearIII ON qryBookingDayswithYear.CustomerID = qryBookingDayswithYearIII.CustomerID
WHERE cDate(NZ([qryBookingDayswithYearIII].[DayRecorded], #1/1/1950#))=Date()
GROUP BY qryBookingDayswithYearIII.DayRecorded, qryBookingDayswithYearIII.CustomerID, qryBookingDayswithYear.[FirstDay], "DAY";
Always like a challenge, and working with queries rather than tables presents that, because you never know what those other queries are doing to your data types.

Did you try it as:

SELECT qryBookingDayswithYearIII.DayRecorded
, qryBookingDayswithYearIII.CustomerID
, qryBookingDayswithYearIII.SumOfExtendedPrice
, qryBookingDayswithYear.[FirstDay] AS DateField
, "DAY" AS DateSpec
, cdate(Nz([qryBookingDayswithYearIII].[DayRecorded],#1/1/1950#)) AS TestDate
FROM qryBookingDayswithYear
INNER JOIN qryBookingDayswithYearIII
ON qryBookingDayswithYear.CustomerID = qryBookingDayswithYearIII.CustomerID
WHERE cDate(NZ([qryBookingDayswithYearIII].[DayRecorded], #1/1/1950#))=Date()

Have you tried simply browsing through [qryBookingDayswithYearIII] and looking at the data in your [DayRecorded] field?  Does the DayRecorded field line up left or right in the field (left means string, right means date or number)

Try this:

SELECT LEN([qryBookingDayswithYearIII].[DayRecorded] & "") as FldLen, Count(*) as Freq
FROM [qryBookingDayswithYearIII]
GROUP BY LEN([qryBookingDayswithYearIII].[DayRecorded] & "")

This might help us identify that that field has some unusual values if there are lengths numbers less than 6 and <> 0.
Still no luck on the main query. The 2nd query came back with these results:

FldLen     Freq
0                328
8                2006

I checked the qryBookingDayswithYearIII and the alignment is: CustomerID is left, DayRecorded is Left, BookNumber is Right, SumofExtendedPrice is Right.
So, what happens when you just do:

SELECT qryBookingDayswithYearIII.DayRecorded
, NZ([qryBookingDayswithYearIII].[DayRecorded], #1/1/1950#) as Test1
FROM qryBookingDayswithYearIII

Does that work?  how do the fields line up in that query?  If that runs, without error, then try

SELECT qryBookingDayswithYearIII.DayRecorded
, NZ([qryBookingDayswithYearIII].[DayRecorded], #1/1/1950#) as Test1
, cdate(NZ([qryBookingDayswithYearIII].[DayRecorded], #1/1/1950#)) as Test2
FROM qryBookingDayswithYearIII

and then, for grins, try:

SELECT qryBookingDayswithYearIII.DayRecorded
, NZ([qryBookingDayswithYearIII].[DayRecorded], "1/1/1950") as Test1
, cdate(NZ([qryBookingDayswithYearIII].[DayRecorded], "1/1/1950")) as Test2
FROM qryBookingDayswithYearIII
All three ran with out error. As far as allignment:

1st query: both columns to the left


2nd query: 1st Column to the left (DayRecorded); 2nd Column (Test1) to the Left; 3rd Column (Test2) to the right.


3rd query: same alignment as query 2.
OK, so lets try this:

SELECT qryBookingDayswithYearIII.DayRecorded
, qryBookingDayswithYearIII.CustomerID
, qryBookingDayswithYearIII.SumOfExtendedPrice
, qryBookingDayswithYear.[FirstDay] AS DateField
, "DAY" AS DateSpec
, cdate(Nz([qryBookingDayswithYearIII].[DayRecorded],"1/1/1950")) AS TestDate
FROM qryBookingDayswithYear
INNER JOIN qryBookingDayswithYearIII
ON qryBookingDayswithYear.CustomerID = qryBookingDayswithYearIII.CustomerID
WHERE cDate(NZ([qryBookingDayswithYearIII].[DayRecorded], "1/1/1950"))=Date()
Darn it... same. Data type mismatch.
OK, I'm going to ask for some help from some other experts.  They have probably backed off since I was engaged, but I am now stumped.
Thanks Dale... really appreciate your help on this.
Run this and check for False:

SELECT
  DayRecorded,
  IsDate([DayRecorded]) As ItIsADate
FROM
  qryBookingDayswithYearIII

/gustav
Comes back with all 2,334 records. The "ItsADate" is all -1. Except the rows that have no date in them. For those, it's a 0.
Try going about it this way:
SELECT qryBookingDayswithYearIII.DayRecorded
, qryBookingDayswithYearIII.CustomerID
, qryBookingDayswithYearIII.SumOfExtendedPrice
, qryBookingDayswithYear.[FirstDay] AS DateField
, "DAY" AS DateSpec
, cdate(Nz([qryBookingDayswithYearIII].[DayRecorded],"1/1/1950")) AS TestDate
FROM qryBookingDayswithYear
INNER JOIN qryBookingDayswithYearIII
ON qryBookingDayswithYear.CustomerID = qryBookingDayswithYearIII.CustomerID
WHERE DateValue(cDate(NZ([qryBookingDayswithYearIII].[DayRecorded], "1/1/1950")))=DateValue(Now()) 

Open in new window


If that works, go into your VBA window (<Alt>+<F11>) and look at the order of the Tools --> References and make sure that Access ones are first. Even if it doesn't work still check them oout. Then do a compact and repair.
DateValue() is an Excel function, not an Access function.
Possible solutions:
Use Date()
SELECT qryBookingDayswithYearIII.DayRecorded, qryBookingDayswithYearIII.CustomerID, Avg(qryBookingDayswithYearIII.SumOfExtendedPrice) AS AvgOfSumOfExtendedPrice, ([FirstDay]) AS DateField, "DAY" AS DateSpec
FROM qryBookingDayswithYear INNER JOIN qryBookingDayswithYearIII ON qryBookingDayswithYear.CustomerID = qryBookingDayswithYearIII.CustomerID
WHERE qryBookingDayswithYearIII.DayRecorded=Date()
GROUP BY qryBookingDayswithYearIII.DayRecorded, qryBookingDayswithYearIII.CustomerID, ([FirstDay]), "DAY";

Open in new window

Use a dynamic query, concatenating the DateValue() function
strSQL = "SELECT qryBookingDayswithYearIII.DayRecorded, qryBookingDayswithYearIII.CustomerID, Avg(qryBookingDayswithYearIII.SumOfExtendedPrice) AS AvgOfSumOfExtendedPrice, ([FirstDay]) AS DateField, ""DAY"" AS DateSpec
FROM qryBookingDayswithYear INNER JOIN qryBookingDayswithYearIII ON qryBookingDayswithYear.CustomerID = qryBookingDayswithYearIII.CustomerID
WHERE qryBookingDayswithYearIII.DayRecorded =#" & DateValue(Now()) & "# 
GROUP BY qryBookingDayswithYearIII.DayRecorded, qryBookingDayswithYearIII.CustomerID, ([FirstDay]), ""DAY"";"

Open in new window

Jim... the query didn't work. Not sure how to priorities those. Which are the "Access ones"?
These are the common ones.
User generated imageAnd then do the Compact.
> Comes back with all 2,334 records. The "ItsADate" is all -1.
> Except the rows that have no date in them. For those, it's a 0.

Yes, and these cannot be converted, thus the data mismatch error.

To end your sorrows, filter on that column: True

/gustav
Thanks Gustav... much appreciated. What exactly do I do?
ASKER CERTIFIED SOLUTION
Avatar of Gustav Brock
Gustav Brock
Flag of Denmark image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
aikimark

Sorry to disappoint, but DateValue( ) is also an Access function.   It converts a value in date/time format by stripping the decimal value basically returns the mm/dd/yy 12:00:00 as a date.

;-)

Has been since at least 2007, cannot remember whether it was available in 2003 or not.
It is from Access 1.0.
/gustav
@cansevin: you can save a lot of time if upload sample DB with some dummy data. You can save your queries as tables if you change query type to Make table and run it. Copy created tables into empty DB, name tables like your queries and upload
Thanks!!
You are welcome!

/gustav