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. DayRecorde d, qryBookingDayswithYearIII. CustomerID , Avg(qryBookingDayswithYear III.SumOfE xtendedPri ce) AS AvgOfSumOfExtendedPrice, ([FirstDay]) AS DateField, "DAY" AS DateSpec
FROM qryBookingDayswithYear INNER JOIN qryBookingDayswithYearIII ON qryBookingDayswithYear.Cus tomerID = qryBookingDayswithYearIII. CustomerID
WHERE (((qryBookingDayswithYearI II.DayReco rded)=Date Value(Now( ))))
GROUP BY qryBookingDayswithYearIII. DayRecorde d, 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([qryBookin gDayswithY earIII].[D ayRecorded ],#1/1/195 0#)))=Date Value(Now( ))))
SELECT qryBookingDayswithYearIII.
FROM qryBookingDayswithYear INNER JOIN qryBookingDayswithYearIII ON qryBookingDayswithYear.Cus
WHERE (((qryBookingDayswithYearI
GROUP BY qryBookingDayswithYearIII.
I also used a "WHERE" clause as below. That pulled up a warning "Data type mismatch in criteria expression"
WHERE (((DateValue(Nz([qryBookin
What DataType is [DayRecorded]?
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([qry BookingDay swithYearI II].[DayRe corded],#1 /1/1950#))
or possibly:
TestDate: cdate(Nz([qryBookingDayswi thYearIII] .[DayRecor ded],#1/1/ 1950#))
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([qry
or possibly:
TestDate: cdate(Nz([qryBookingDayswi
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"
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"
ASKER
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!
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([qryBookingDa yswithYear III].[DayR ecorded],# 1/1/1950#) = Date()
WHERE DateValue(Nz([qryBookingDa
ASKER
Warning pops up..
Syntax error (missing operator) in query expression 'DateValue(Nz([qryBookingD ayswithYea rIII].[Day Recorded], #1/1/1950# ) = Date() GROUP BY qryBookingDayswithYeearIII .DayRecord ed, qryBookingDayswithYEarIII. CustomerID , ([FirstDay]), "DAY";'.
Syntax error (missing operator) in query expression 'DateValue(Nz([qryBookingD
Looks like you may be missing a ")" to close out the DateValue( ), right before the = in the where clause.
Try:
SELECT qryBookingDayswithYearIII. DayRecorde d
, qryBookingDayswithYearIII. CustomerID
, Avg(qryBookingDayswithYear III.SumOfE xtendedPri ce) AS AvgOfSumOfExtendedPrice
, [FirstDay] AS DateField
, "DAY" AS DateSpec
FROM qryBookingDayswithYear
INNER JOIN qryBookingDayswithYearIII
ON qryBookingDayswithYear.Cus tomerID = qryBookingDayswithYearIII. CustomerID
WHERE DateValue(NZ([qryBookingDa yswithYear III].[DayR ecorded], #1/1/1950#))=Date()
GROUP BY qryBookingDayswithYearIII. DayRecorde d
, qryBookingDayswithYearIII. CustomerID
, [FirstDay]
, "DAY";
Try:
SELECT qryBookingDayswithYearIII.
, qryBookingDayswithYearIII.
, Avg(qryBookingDayswithYear
, [FirstDay] AS DateField
, "DAY" AS DateSpec
FROM qryBookingDayswithYear
INNER JOIN qryBookingDayswithYearIII
ON qryBookingDayswithYear.Cus
WHERE DateValue(NZ([qryBookingDa
GROUP BY qryBookingDayswithYearIII.
, qryBookingDayswithYearIII.
, [FirstDay]
, "DAY";
ASKER
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?
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?
ASKER
Yes... there are two customer ID's. Both of them line up to the left.
Have you tried:
SELECT qryBookingDayswithYearIII. DayRecorde d
, qryBookingDayswithYearIII. CustomerID
, qryBookingDayswithYearIII. SumOfExten dedPrice
, [FirstDay] AS DateField
, "DAY" AS DateSpec
FROM qryBookingDayswithYear
INNER JOIN qryBookingDayswithYearIII
ON qryBookingDayswithYear.Cus tomerID = qryBookingDayswithYearIII. CustomerID
WHERE DateValue(NZ([qryBookingDa yswithYear III].[DayR ecorded], #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?
SELECT qryBookingDayswithYearIII.
, qryBookingDayswithYearIII.
, qryBookingDayswithYearIII.
, [FirstDay] AS DateField
, "DAY" AS DateSpec
FROM qryBookingDayswithYear
INNER JOIN qryBookingDayswithYearIII
ON qryBookingDayswithYear.Cus
WHERE DateValue(NZ([qryBookingDa
Still don't know what [FirstDay] is? or which of the queries it comes from. Why doesn't that field have a query identifier?
ASKER
Ahh man, this sucks!
That comes from the qryBookingDayswithYear.[Fi rstDay]
It still comes up as data type mismatch. It works fine with out the where, but the where makes it not work.
That comes from the qryBookingDayswithYear.[Fi
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([qryBookingDayswi thYearIII] .[DayRecor ded], #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.
WHERE cDate(NZ([qryBookingDayswi
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.
ASKER
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. DayRecorde d, qryBookingDayswithYearIII. CustomerID , Avg(qryBookingDayswithYear III.SumOfE xtendedPri ce) AS AvgOfSumOfExtendedPrice, qryBookingDayswithYear.[Fi rstDay] AS DateField, "DAY" AS DateSpec, DateValue(Nz([qryBookingDa yswithYear III].[DayR ecorded],# 1/1/1950#) ) AS TestDate
FROM qryBookingDayswithYear INNER JOIN qryBookingDayswithYearIII ON qryBookingDayswithYear.Cus tomerID = qryBookingDayswithYearIII. CustomerID
WHERE cDate(NZ([qryBookingDayswi thYearIII] .[DayRecor ded], #1/1/1950#))=Date()
GROUP BY qryBookingDayswithYearIII. DayRecorde d, qryBookingDayswithYearIII. CustomerID , qryBookingDayswithYear.[Fi rstDay], "DAY";
SELECT qryBookingDayswithYearIII.
FROM qryBookingDayswithYear INNER JOIN qryBookingDayswithYearIII ON qryBookingDayswithYear.Cus
WHERE cDate(NZ([qryBookingDayswi
GROUP BY qryBookingDayswithYearIII.
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. DayRecorde d
, qryBookingDayswithYearIII. CustomerID
, qryBookingDayswithYearIII. SumOfExten dedPrice
, qryBookingDayswithYear.[Fi rstDay] AS DateField
, "DAY" AS DateSpec
, cdate(Nz([qryBookingDayswi thYearIII] .[DayRecor ded],#1/1/ 1950#)) AS TestDate
FROM qryBookingDayswithYear
INNER JOIN qryBookingDayswithYearIII
ON qryBookingDayswithYear.Cus tomerID = qryBookingDayswithYearIII. CustomerID
WHERE cDate(NZ([qryBookingDayswi thYearIII] .[DayRecor ded], #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([qryBookingDayswithYea rIII].[Day Recorded] & "") as FldLen, Count(*) as Freq
FROM [qryBookingDayswithYearIII ]
GROUP BY LEN([qryBookingDayswithYea rIII].[Day Recorded] & "")
This might help us identify that that field has some unusual values if there are lengths numbers less than 6 and <> 0.
Did you try it as:
SELECT qryBookingDayswithYearIII.
, qryBookingDayswithYearIII.
, qryBookingDayswithYearIII.
, qryBookingDayswithYear.[Fi
, "DAY" AS DateSpec
, cdate(Nz([qryBookingDayswi
FROM qryBookingDayswithYear
INNER JOIN qryBookingDayswithYearIII
ON qryBookingDayswithYear.Cus
WHERE cDate(NZ([qryBookingDayswi
Have you tried simply browsing through [qryBookingDayswithYearIII
Try this:
SELECT LEN([qryBookingDayswithYea
FROM [qryBookingDayswithYearIII
GROUP BY LEN([qryBookingDayswithYea
This might help us identify that that field has some unusual values if there are lengths numbers less than 6 and <> 0.
ASKER
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.
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. DayRecorde d
, NZ([qryBookingDayswithYear III].[DayR ecorded], #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. DayRecorde d
, NZ([qryBookingDayswithYear III].[DayR ecorded], #1/1/1950#) as Test1
, cdate(NZ([qryBookingDayswi thYearIII] .[DayRecor ded], #1/1/1950#)) as Test2
FROM qryBookingDayswithYearIII
and then, for grins, try:
SELECT qryBookingDayswithYearIII. DayRecorde d
, NZ([qryBookingDayswithYear III].[DayR ecorded], "1/1/1950") as Test1
, cdate(NZ([qryBookingDayswi thYearIII] .[DayRecor ded], "1/1/1950")) as Test2
FROM qryBookingDayswithYearIII
SELECT qryBookingDayswithYearIII.
, NZ([qryBookingDayswithYear
FROM qryBookingDayswithYearIII
Does that work? how do the fields line up in that query? If that runs, without error, then try
SELECT qryBookingDayswithYearIII.
, NZ([qryBookingDayswithYear
, cdate(NZ([qryBookingDayswi
FROM qryBookingDayswithYearIII
and then, for grins, try:
SELECT qryBookingDayswithYearIII.
, NZ([qryBookingDayswithYear
, cdate(NZ([qryBookingDayswi
FROM qryBookingDayswithYearIII
ASKER
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.
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. DayRecorde d
, qryBookingDayswithYearIII. CustomerID
, qryBookingDayswithYearIII. SumOfExten dedPrice
, qryBookingDayswithYear.[Fi rstDay] AS DateField
, "DAY" AS DateSpec
, cdate(Nz([qryBookingDayswi thYearIII] .[DayRecor ded],"1/1/ 1950")) AS TestDate
FROM qryBookingDayswithYear
INNER JOIN qryBookingDayswithYearIII
ON qryBookingDayswithYear.Cus tomerID = qryBookingDayswithYearIII. CustomerID
WHERE cDate(NZ([qryBookingDayswi thYearIII] .[DayRecor ded], "1/1/1950"))=Date()
SELECT qryBookingDayswithYearIII.
, qryBookingDayswithYearIII.
, qryBookingDayswithYearIII.
, qryBookingDayswithYear.[Fi
, "DAY" AS DateSpec
, cdate(Nz([qryBookingDayswi
FROM qryBookingDayswithYear
INNER JOIN qryBookingDayswithYearIII
ON qryBookingDayswithYear.Cus
WHERE cDate(NZ([qryBookingDayswi
ASKER
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.
ASKER
Thanks Dale... really appreciate your help on this.
Run this and check for False:
SELECT
DayRecorded,
IsDate([DayRecorded]) As ItIsADate
FROM
qryBookingDayswithYearIII
/gustav
SELECT
DayRecorded,
IsDate([DayRecorded]) As ItIsADate
FROM
qryBookingDayswithYearIII
/gustav
ASKER
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:
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.
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())
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()
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";
Use a dynamic query, concatenating the DateValue() functionstrSQL = "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"";"
ASKER
Jim... the query didn't work. Not sure how to priorities those. Which are the "Access ones"?
> 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
> 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
ASKER
Thanks Gustav... much appreciated. What exactly do I do?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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.
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
/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
ASKER
Thanks!!
You are welcome!
/gustav
/gustav