Solved

Where clause in query

Posted on 2014-01-23
37
291 Views
Last Modified: 2014-01-25
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())))
0
Comment
Question by:cansevin
  • 15
  • 12
  • 5
  • +4
37 Comments
 
LVL 84
Comment Utility
What DataType is [DayRecorded]?
0
 

Author Comment

by:cansevin
Comment Utility
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.
0
 
LVL 47

Expert Comment

by:Dale Fye (Access MVP)
Comment Utility
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#))
0
 
LVL 47

Expert Comment

by:Dale Fye (Access MVP)
Comment Utility
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"
0
 

Author Comment

by:cansevin
Comment Utility
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!
0
 
LVL 47

Expert Comment

by:Dale Fye (Access MVP)
Comment Utility
Given that the first "TestDate" works, try a WHERE clause of:

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

Author Comment

by:cansevin
Comment Utility
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";'.
0
 
LVL 47

Expert Comment

by:Dale Fye (Access MVP)
Comment Utility
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";
0
 

Author Comment

by:cansevin
Comment Utility
Thanks... but now it is back to "Data Type Mismatch in query expression"
0
 
LVL 47

Expert Comment

by:Dale Fye (Access MVP)
Comment Utility
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?
0
 

Author Comment

by:cansevin
Comment Utility
Yes... there are two customer ID's. Both of them line up to the left.
0
 
LVL 47

Expert Comment

by:Dale Fye (Access MVP)
Comment Utility
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?
0
 

Author Comment

by:cansevin
Comment Utility
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.
0
 
LVL 47

Expert Comment

by:Dale Fye (Access MVP)
Comment Utility
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.
0
 

Author Comment

by:cansevin
Comment Utility
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";
0
 
LVL 47

Expert Comment

by:Dale Fye (Access MVP)
Comment Utility
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.
0
 

Author Comment

by:cansevin
Comment Utility
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.
0
 
LVL 47

Expert Comment

by:Dale Fye (Access MVP)
Comment Utility
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
0
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Author Comment

by:cansevin
Comment Utility
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.
0
 
LVL 47

Expert Comment

by:Dale Fye (Access MVP)
Comment Utility
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()
0
 

Author Comment

by:cansevin
Comment Utility
Darn it... same. Data type mismatch.
0
 
LVL 47

Expert Comment

by:Dale Fye (Access MVP)
Comment Utility
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.
0
 

Author Comment

by:cansevin
Comment Utility
Thanks Dale... really appreciate your help on this.
0
 
LVL 49

Expert Comment

by:Gustav Brock
Comment Utility
Run this and check for False:

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

/gustav
0
 

Author Comment

by:cansevin
Comment Utility
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.
0
 
LVL 38

Expert Comment

by:Jim P.
Comment Utility
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.
0
 
LVL 45

Expert Comment

by:aikimark
Comment Utility
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

0
 

Author Comment

by:cansevin
Comment Utility
Jim... the query didn't work. Not sure how to priorities those. Which are the "Access ones"?
0
 
LVL 38

Expert Comment

by:Jim P.
Comment Utility
These are the common ones.
Common ReferencesAnd then do the Compact.
0
 
LVL 49

Expert Comment

by:Gustav Brock
Comment Utility
> 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
0
 

Author Comment

by:cansevin
Comment Utility
Thanks Gustav... much appreciated. What exactly do I do?
0
 
LVL 49

Accepted Solution

by:
Gustav Brock earned 500 total points
Comment Utility
You filter on that expression:

SELECT
  *
FROM
  qryBookingDayswithYearIII
WHERE
  IsDate([DayRecorded]) = True

Save this a query and use this query in your origianal query in place of qryBookingDayswithYearIII.

/gustav
0
 
LVL 47

Expert Comment

by:Dale Fye (Access MVP)
Comment Utility
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.
0
 
LVL 49

Expert Comment

by:Gustav Brock
Comment Utility
It is from Access 1.0.
/gustav
0
 
LVL 39

Expert Comment

by:als315
Comment Utility
@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
0
 

Author Closing Comment

by:cansevin
Comment Utility
Thanks!!
0
 
LVL 49

Expert Comment

by:Gustav Brock
Comment Utility
You are welcome!

/gustav
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Experts-Exchange is a great place to come for help with solutions for your database issues, and many problems are resolved within minutes of being posted.  Others take a little more time and effort and often providing a sample database is very helpf…
In a multiple monitor setup, if you don't want to use AutoCenter to position your popup forms, you have a problem: where will they appear?  Sometimes you may have an additional problem: where the devil did they go?  If you last had a popup form open…
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
What’s inside an Access Desktop Database. Will look at the basic interface, Navigation Pane (Database Container), Tables, Queries, Forms, Report, Macro’s, and VBA code.

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now