PC Review


Reply
Thread Tools Rate Thread

DateAdd and Month functions

 
 
Ivan Pavlov
Guest
Posts: n/a
 
      20th Oct 2010
Can someone please explain to me why the following SQL works in
Access:

SELECT JIRA,Title,Duration, [Assigned Date],Month(DateAdd("d",
[Duration], [Assigned Date]))
FROM Issues
WHERE Month([Assigned Date])=8
ORDER by Month(DateAdd("d", [Duration], [Assigned Date])) desc

But the following:

SELECT JIRA,Title,Duration, [Assigned Date],Month(DateAdd("d",
[Duration], [Assigned Date]))
FROM Issues
WHERE Month([Assigned Date])=8 And Month(DateAdd("d", [Duration],
[Assigned Date]))=9

returns "type mismatch"?

How can I include Month(DateAdd("d", [Duration], [Assigned Date])) in
where clause?
 
Reply With Quote
 
 
 
 
Bob Quintal
Guest
Posts: n/a
 
      20th Oct 2010
Ivan Pavlov <(E-Mail Removed)> wrote in news:126a4e41-4e75-4cfb-
ba98-(E-Mail Removed):

> Can someone please explain to me why the following SQL works in
> Access:
>
> SELECT JIRA,Title,Duration, [Assigned Date],Month(DateAdd("d",
> [Duration], [Assigned Date]))
> FROM Issues
> WHERE Month([Assigned Date])=8
> ORDER by Month(DateAdd("d", [Duration], [Assigned Date])) desc
>
> But the following:
>
> SELECT JIRA,Title,Duration, [Assigned Date],Month(DateAdd("d",
> [Duration], [Assigned Date]))
> FROM Issues
> WHERE Month([Assigned Date])=8 And Month(DateAdd("d", [Duration],
> [Assigned Date]))=9
>
> returns "type mismatch"?
>
> How can I include Month(DateAdd("d", [Duration], [Assigned Date]))

in
> where clause?
>

x = 8 And x = 9 = 0
try changing the And to Or


--
Bob Q.
PA is y I've altered my address.
 
Reply With Quote
 
John Spencer
Guest
Posts: n/a
 
      20th Oct 2010
If duration is ever blank (null or zero length string) or not a number then
your expression will generate an error - invalid use of null or type mismatch.

I suspect that is the cause of your problem.

You might be able to use something like the following to avoid the error. I
don't know if it will return what you want when duration is not a number value.
DateAdd("D",IIF(IsNumeric([Duration]),[Duration],0),[Assigned Date]) = 9


John Spencer
Access MVP 2002-2005, 2007-2010
The Hilltop Institute
University of Maryland Baltimore County

Ivan Pavlov wrote:
> Can someone please explain to me why the following SQL works in
> Access:
>
> SELECT JIRA,Title,Duration, [Assigned Date],Month(DateAdd("d",
> [Duration], [Assigned Date]))
> FROM Issues
> WHERE Month([Assigned Date])=8
> ORDER by Month(DateAdd("d", [Duration], [Assigned Date])) desc
>
> But the following:
>
> SELECT JIRA,Title,Duration, [Assigned Date],Month(DateAdd("d",
> [Duration], [Assigned Date]))
> FROM Issues
> WHERE Month([Assigned Date])=8 And Month(DateAdd("d", [Duration],
> [Assigned Date]))=9
>
> returns "type mismatch"?
>
> How can I include Month(DateAdd("d", [Duration], [Assigned Date])) in
> where clause?

 
Reply With Quote
 
Ivan Pavlov
Guest
Posts: n/a
 
      23rd Oct 2010
Thanks for the answer. The problem however is not in the calculation
of Month(DateAdd("d", [Duration], [Assigned Date])). This is fine as
you can see because it is in the SELECT-ed expressions. The problem
comes when I try to compare it with an integer - 9 in my case - then I
get a type mismatch. Even if I use Cint on the expression it still
produces the same error.

It is really frustrating...


On Oct 20, 7:33*pm, John Spencer <JSPEN...@Hilltop.umbc> wrote:
> If duration is ever blank (null or zero length string) or not a number then
> your expression will generate an error - invalid use of null or type mismatch.
>
> I suspect that is the cause of your problem.
>
> You might be able to use something like the following to avoid the error.*I
> don't know if it will return what you want when duration is not a number value.
> DateAdd("D",IIF(IsNumeric([Duration]),[Duration],0),[Assigned Date]) = 9
>
> John Spencer
> Access MVP 2002-2005, 2007-2010
> The Hilltop Institute
> University of Maryland Baltimore County
>
>
>
> Ivan Pavlov wrote:
> > Can someone please explain to me why the following SQL works in
> > Access:

>
> > SELECT JIRA,Title,Duration, [Assigned Date],Month(DateAdd("d",
> > [Duration], [Assigned Date]))
> > FROM Issues
> > WHERE Month([Assigned Date])=8
> > ORDER by Month(DateAdd("d", [Duration], [Assigned Date])) desc

>
> > But the following:

>
> > SELECT JIRA,Title,Duration, [Assigned Date],Month(DateAdd("d",
> > [Duration], [Assigned Date]))
> > FROM Issues
> > WHERE Month([Assigned Date])=8 And Month(DateAdd("d", [Duration],
> > [Assigned Date]))=9

>
> > returns "type mismatch"?

>
> > How can I include Month(DateAdd("d", [Duration], [Assigned Date])) in
> > where clause?


 
Reply With Quote
 
John W. Vinson
Guest
Posts: n/a
 
      23rd Oct 2010
On Sat, 23 Oct 2010 13:53:03 -0700 (PDT), Ivan Pavlov <(E-Mail Removed)>
wrote:

>Thanks for the answer. The problem however is not in the calculation
>of Month(DateAdd("d", [Duration], [Assigned Date])). This is fine as
>you can see because it is in the SELECT-ed expressions. The problem
>comes when I try to compare it with an integer - 9 in my case - then I
>get a type mismatch. Even if I use Cint on the expression it still
>produces the same error.


One thing to check: do you have a private function named Month, or a field or
a control on a form? It's possible that Access isn't in fact hitting the
builtin Month() function.
--

John W. Vinson [MVP]
Microsoft's replacements for these newsgroups:
http://social.msdn.microsoft.com/For...-US/accessdev/
http://social.answers.microsoft.com/.../en-US/addbuz/
and see also http://www.utteraccess.com
 
Reply With Quote
 
John Spencer
Guest
Posts: n/a
 
      24th Oct 2010
Is DURATION ever Null? If so, the DateAdd function will return an error.
This is not necessarily a problem in a select clause if no record is returned
where duration is null.

In a where clause, every record may be checked and if even one record returns
an error, then the data engine will return a mismatch error.

So you need to change the where clause. You can try
WHERE Month([Assigned Date])=8
And IIF(Duration is Null,0,Month(DateAdd("d", [Duration],[Assigned Date])))=9

John Spencer
Access MVP 2002-2005, 2007-2010
The Hilltop Institute
University of Maryland Baltimore County

John W. Vinson wrote:
> On Sat, 23 Oct 2010 13:53:03 -0700 (PDT), Ivan Pavlov <(E-Mail Removed)>
> wrote:
>
>> Thanks for the answer. The problem however is not in the calculation
>> of Month(DateAdd("d", [Duration], [Assigned Date])). This is fine as
>> you can see because it is in the SELECT-ed expressions. The problem
>> comes when I try to compare it with an integer - 9 in my case - then I
>> get a type mismatch. Even if I use Cint on the expression it still
>> produces the same error.

>
> One thing to check: do you have a private function named Month, or a field or
> a control on a form? It's possible that Access isn't in fact hitting the
> builtin Month() function.

 
Reply With Quote
 
Ivan Pavlov
Guest
Posts: n/a
 
      25th Oct 2010
On Oct 24, 3:16*pm, John Spencer <JSPEN...@Hilltop.umbc> wrote:
> Is DURATION ever Null? *If so, the DateAdd function will return an error.
> This is not necessarily a problem in a select clause if no record is returned
> where duration is null.
>
> In a where clause, every record may be checked and if even one record returns
> an error, then the data engine will return a mismatch error.
>
> So you need to change the where clause. You can try
> WHERE Month([Assigned Date])=8
> And IIF(Duration is Null,0,Month(DateAdd("d", [Duration],[Assigned Date])))=9
>
> John Spencer
> Access MVP 2002-2005, 2007-2010
> The Hilltop Institute
> University of Maryland Baltimore County
>
> John W. Vinson wrote:
> > On Sat, 23 Oct 2010 13:53:03 -0700 (PDT), Ivan Pavlov <ivan.pav...@gmail.com>
> > wrote:

>
> >> Thanks for the answer. The problem however is not in the calculation
> >> of Month(DateAdd("d", [Duration], [Assigned Date])). This is fine as
> >> you can see because it is in the SELECT-ed expressions. The problem
> >> comes when I try to compare it with an integer - 9 in my case - then I
> >> get a type mismatch. Even if I use Cint on the expression it still
> >> produces the same error.

>
> > One thing to check: do you have a private function named Month, or a field or
> > a control on a form? It's possible that Access isn't in fact hitting the
> > builtin Month() function.


Thank you very much!!! Indeed this was the problem!
I simply can't get used to all Access oddities.
 
Reply With Quote
 
John W. Vinson
Guest
Posts: n/a
 
      25th Oct 2010
On Mon, 25 Oct 2010 06:38:18 -0700 (PDT), Ivan Pavlov <(E-Mail Removed)>
wrote:

>On Oct 24, 3:16*pm, John Spencer <JSPEN...@Hilltop.umbc> wrote:
>> Is DURATION ever Null? *If so, the DateAdd function will return an error.
>> This is not necessarily a problem in a select clause if no record is returned
>> where duration is null.
>>
>> In a where clause, every record may be checked and if even one record returns
>> an error, then the data engine will return a mismatch error.
>>
>> So you need to change the where clause. You can try
>> WHERE Month([Assigned Date])=8
>> And IIF(Duration is Null,0,Month(DateAdd("d", [Duration],[Assigned Date])))=9
>>
>> John Spencer
>> Access MVP 2002-2005, 2007-2010
>> The Hilltop Institute
>> University of Maryland Baltimore County
>>
>> John W. Vinson wrote:
>> > On Sat, 23 Oct 2010 13:53:03 -0700 (PDT), Ivan Pavlov <ivan.pav...@gmail.com>
>> > wrote:

>>
>> >> Thanks for the answer. The problem however is not in the calculation
>> >> of Month(DateAdd("d", [Duration], [Assigned Date])). This is fine as
>> >> you can see because it is in the SELECT-ed expressions. The problem
>> >> comes when I try to compare it with an integer - 9 in my case - then I
>> >> get a type mismatch. Even if I use Cint on the expression it still
>> >> produces the same error.

>>
>> > One thing to check: do you have a private function named Month, or a field or
>> > a control on a form? It's possible that Access isn't in fact hitting the
>> > builtin Month() function.

>
>Thank you very much!!! Indeed this was the problem!
>I simply can't get used to all Access oddities.


Well, I don't really see that as an "Access oddity" - unless you can tell me
the exact date of <mmpf> months from today. <g>
--

John W. Vinson [MVP]
Microsoft's replacements for these newsgroups:
http://social.msdn.microsoft.com/For...-US/accessdev/
http://social.answers.microsoft.com/.../en-US/addbuz/
and see also http://www.utteraccess.com
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
DateAdd and other functions desolationstorm Microsoft Access Queries 3 6th Nov 2009 05:26 PM
Add a day and a month with DateAdd Michelle Microsoft Access Queries 4 23rd Jul 2009 07:49 PM
DateAdd Function for and ENTIRE Month CBender Microsoft Access Queries 3 10th Mar 2009 12:25 PM
dateadd on month =?Utf-8?B?c2FtdWVs?= Microsoft Access Queries 6 4th Sep 2007 08:36 PM
Help with VBA (or VB) DateAdd, WORDAY functions, date format =?Utf-8?B?S2luZCB3cml0ZXIvdXNlci9wcm9ncmFtbWVy?= Microsoft Excel Programming 7 8th Jan 2005 10:53 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 12:49 AM.