Solved

Where clause in query

Posted on 2014-01-23
37
302 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 15
  • 12
  • 5
  • +4
37 Comments
 
LVL 85
ID: 39803349
What DataType is [DayRecorded]?
0
 

Author Comment

by:cansevin
ID: 39803389
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 48

Expert Comment

by:Dale Fye
ID: 39803412
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
Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

 
LVL 48

Expert Comment

by:Dale Fye
ID: 39803435
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
ID: 39803437
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 48

Expert Comment

by:Dale Fye
ID: 39803489
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
ID: 39803524
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 48

Expert Comment

by:Dale Fye
ID: 39803740
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
ID: 39803782
Thanks... but now it is back to "Data Type Mismatch in query expression"
0
 
LVL 48

Expert Comment

by:Dale Fye
ID: 39803836
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
ID: 39803940
Yes... there are two customer ID's. Both of them line up to the left.
0
 
LVL 48

Expert Comment

by:Dale Fye
ID: 39803958
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
ID: 39804008
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 48

Expert Comment

by:Dale Fye
ID: 39804047
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
ID: 39804061
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 48

Expert Comment

by:Dale Fye
ID: 39804125
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
ID: 39804185
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 48

Expert Comment

by:Dale Fye
ID: 39804222
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
 

Author Comment

by:cansevin
ID: 39804234
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 48

Expert Comment

by:Dale Fye
ID: 39804241
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
ID: 39804255
Darn it... same. Data type mismatch.
0
 
LVL 48

Expert Comment

by:Dale Fye
ID: 39804385
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
ID: 39804403
Thanks Dale... really appreciate your help on this.
0
 
LVL 51

Expert Comment

by:Gustav Brock
ID: 39804500
Run this and check for False:

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

/gustav
0
 

Author Comment

by:cansevin
ID: 39804605
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.
ID: 39804657
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 46

Expert Comment

by:aikimark
ID: 39804675
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
ID: 39804699
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.
ID: 39804756
These are the common ones.
Common ReferencesAnd then do the Compact.
0
 
LVL 51

Expert Comment

by:Gustav Brock
ID: 39804777
> 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
ID: 39804801
Thanks Gustav... much appreciated. What exactly do I do?
0
 
LVL 51

Accepted Solution

by:
Gustav Brock earned 500 total points
ID: 39805052
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 48

Expert Comment

by:Dale Fye
ID: 39805093
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 51

Expert Comment

by:Gustav Brock
ID: 39806004
It is from Access 1.0.
/gustav
0
 
LVL 40

Expert Comment

by:als315
ID: 39807263
@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
ID: 39807338
Thanks!!
0
 
LVL 51

Expert Comment

by:Gustav Brock
ID: 39808347
You are welcome!

/gustav
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

AutoNumbers should increment automatically, without duplicates.  But sometimes something goes wrong, and the next AutoNumber value is a duplicate.  This article shows how to recover from this problem.
This article shows how to get a list of available printers for display in a drop-down list, and then to use the selected printer to print an Access report or a Word document filled with Access data, using different syntax as needed for working with …
Basics of query design. Shows you how to construct a simple query by adding tables, perform joins, defining output columns, perform sorting, and apply criteria.
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…

635 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