Solved

Where clause in query

Posted on 2014-01-23
37
300 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 84
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 47

Expert Comment

by:Dale Fye (Access MVP)
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
Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

 
LVL 47

Expert Comment

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

Expert Comment

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

Expert Comment

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

Expert Comment

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

Expert Comment

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

Expert Comment

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

Expert Comment

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

Expert Comment

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

Expert Comment

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

Expert Comment

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

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 45

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 50

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 50

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 47

Expert Comment

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

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 50

Expert Comment

by:Gustav Brock
ID: 39808347
You are welcome!

/gustav
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

In earlier versions of Windows (XP and before), you could drag a database to the taskbar, where it would appear as a taskbar icon to open that database.  This article shows how to recreate this functionality in Windows 7 through 10.
This article describes two methods for creating a combo box that can be used to add new items to the row source -- one for simple lookup tables, and one for a more complex row source where the new item needs data for several fields.
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
With Microsoft Access, learn how to start a database in different ways and produce different start-up actions allowing you to use a single database to perform multiple tasks. Specify a start-up form through options: Specify an Autoexec macro: Us…

738 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