PC Review


Reply
Thread Tools Rate Thread

DateDiff function throwing error in Where clause

 
 
Petr Danes
Guest
Posts: n/a
 
      3rd May 2010
I have a set of queries that generate dates and want to use DateDiff to
select those records with certain dates are greater than others. The
following clause keeps giving me an incompatible data type error:

WHERE DateDiff('d',[From date],[DatumStvoreni])>0

I have the exact expression in the SELECT clause and it show positive and
negative integers just fine, but when I add this test, I get the error.
There are no bad values in the dataset, I've looked, and when I export the
DateDiff column to a temp table and run a query on that, it works fine. Only
when I use the test directly with the DateDiff function does it bomb. I also
tried putting CDate() around the field expressions and it didn't help.

Pete



--
This e-mail address is fake, to keep spammers and their address harvesters
out of my hair. If you want to get in touch personally, I am 'pdanes' and I
use yahoo mail. But please use the newsgroup when possible, so that all may
benefit from the exchange of ideas.


 
Reply With Quote
 
 
 
 
Tom Lake
Guest
Posts: n/a
 
      3rd May 2010

"Petr Danes" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> I have a set of queries that generate dates and want to use DateDiff to select those
> records with certain dates are greater than others. The following clause keeps giving me
> an incompatible data type error:
>
> WHERE DateDiff('d',[From date],[DatumStvoreni])>0
>
> I have the exact expression in the SELECT clause and it show positive and negative
> integers just fine, but when I add this test, I get the error. There are no bad values
> in the dataset, I've looked, and when I export the DateDiff column to a temp table and
> run a query on that, it works fine. Only when I use the test directly with the DateDiff
> function does it bomb. I also tried putting CDate() around the field expressions and it
> didn't help.


I get an error when I use single quotes. I have to use this:

DateDiff("d",[From date],[DatumStvoreni])

The whole expression I tried was this:

WHERE (DateDiff("d",[From date],[DatumStvoreni])>0)

Tom Lake



 
Reply With Quote
 
Petr Danes
Guest
Posts: n/a
 
      3rd May 2010
Tried that as well - no soap. Sometimes I get a message that the query was
too complicated and can't be evaluated. Here's the whole thing:

SELECT Stvoreni_Convert.DatumStvoreni, DateDiff("d",[From
date],[DatumStvoreni]) AS Rozdil, Stvoreni_Convert.AkcesPodrobnostiAutoID
INTO STV
FROM Stvoreni_Convert
WHERE (((DateDiff("d",[From date],[DatumStvoreni]))>0));

The source of that query is this one (Stvoreni_Convert):
SELECT Stvoreni.Stvoreni, getdate([stvoreni]) AS DatumStvoreni,
Stvoreni.AkcesPodrobnostiAutoID
FROM Stvoreni
WHERE (((getdate([stvoreni]))<>''));

and the source of that one is here(Stvoreni):
SELECT IIf(InStrRev(nz([Inventarizace]),Chr(13) &
Chr(10))>0,Mid(nz([Inventarizace]),2+InStrRev(nz([Inventarizace]),Chr(13) &
Chr(10))),nz([Inventarizace])) AS Stvoreni,
AkcesPodrobnosti.AkcesPodrobnostiAutoID
FROM AkcesPodrobnosti
WHERE (((nz([Inventarizace]))<>'') AND ((AkcesPodrobnosti.EvidenceLetter) Is
Not Null));

AkcesPodrobnosti is a table, with Inventarizace as a memo field.
Getdate is a VBA function extracts a date from a mess of text and returns it
as a string.

All the preceding works fine, until I add the WHERE clause in the top query.

Pete


"Tom Lake" <(E-Mail Removed)> píse v diskusním príspevku
news:(E-Mail Removed)...
>
> "Petr Danes" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
>> I have a set of queries that generate dates and want to use DateDiff to
>> select those records with certain dates are greater than others. The
>> following clause keeps giving me an incompatible data type error:
>>
>> WHERE DateDiff('d',[From date],[DatumStvoreni])>0
>>
>> I have the exact expression in the SELECT clause and it show positive and
>> negative integers just fine, but when I add this test, I get the error.
>> There are no bad values in the dataset, I've looked, and when I export
>> the DateDiff column to a temp table and run a query on that, it works
>> fine. Only when I use the test directly with the DateDiff function does
>> it bomb. I also tried putting CDate() around the field expressions and it
>> didn't help.

>
> I get an error when I use single quotes. I have to use this:
>
> DateDiff("d",[From date],[DatumStvoreni])
>
> The whole expression I tried was this:
>
> WHERE (DateDiff("d",[From date],[DatumStvoreni])>0)
>
> Tom Lake
>
>
>



 
Reply With Quote
 
Petr Danes
Guest
Posts: n/a
 
      3rd May 2010
I just tried the last query in another form, using a direct comparison
instead of the DateDiff function. Same error.

SELECT Stvoreni_Convert.DatumStvoreni, CDate([DatumStvoreni]) AS Rozdil,
Stvoreni_Convert.AkcesPodrobnostiAutoID INTO STV
FROM Stvoreni_Convert
WHERE (((CDate([DatumStvoreni]))>CDate([From date])));

Pete



"Tom Lake" <(E-Mail Removed)> píse v diskusním príspevku
news:(E-Mail Removed)...
>
> "Petr Danes" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
>> I have a set of queries that generate dates and want to use DateDiff to
>> select those records with certain dates are greater than others. The
>> following clause keeps giving me an incompatible data type error:
>>
>> WHERE DateDiff('d',[From date],[DatumStvoreni])>0
>>
>> I have the exact expression in the SELECT clause and it show positive and
>> negative integers just fine, but when I add this test, I get the error.
>> There are no bad values in the dataset, I've looked, and when I export
>> the DateDiff column to a temp table and run a query on that, it works
>> fine. Only when I use the test directly with the DateDiff function does
>> it bomb. I also tried putting CDate() around the field expressions and it
>> didn't help.

>
> I get an error when I use single quotes. I have to use this:
>
> DateDiff("d",[From date],[DatumStvoreni])
>
> The whole expression I tried was this:
>
> WHERE (DateDiff("d",[From date],[DatumStvoreni])>0)
>
> Tom Lake
>
>
>



 
Reply With Quote
 
Douglas J. Steele
Guest
Posts: n/a
 
      3rd May 2010
You sure you've got valid values for both fields in every row?

--
Doug Steele, Microsoft Access MVP
http://I.Am/DougSteele
(no private e-mails, please)


"Petr Danes" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
>I just tried the last query in another form, using a direct comparison
>instead of the DateDiff function. Same error.
>
> SELECT Stvoreni_Convert.DatumStvoreni, CDate([DatumStvoreni]) AS Rozdil,
> Stvoreni_Convert.AkcesPodrobnostiAutoID INTO STV
> FROM Stvoreni_Convert
> WHERE (((CDate([DatumStvoreni]))>CDate([From date])));
>
> Pete
>
>
>
> "Tom Lake" <(E-Mail Removed)> píse v diskusním príspevku
> news:(E-Mail Removed)...
>>
>> "Petr Danes" <(E-Mail Removed)> wrote in message
>> news:(E-Mail Removed)...
>>> I have a set of queries that generate dates and want to use DateDiff to
>>> select those records with certain dates are greater than others. The
>>> following clause keeps giving me an incompatible data type error:
>>>
>>> WHERE DateDiff('d',[From date],[DatumStvoreni])>0
>>>
>>> I have the exact expression in the SELECT clause and it show positive
>>> and negative integers just fine, but when I add this test, I get the
>>> error. There are no bad values in the dataset, I've looked, and when I
>>> export the DateDiff column to a temp table and run a query on that, it
>>> works fine. Only when I use the test directly with the DateDiff function
>>> does it bomb. I also tried putting CDate() around the field expressions
>>> and it didn't help.

>>
>> I get an error when I use single quotes. I have to use this:
>>
>> DateDiff("d",[From date],[DatumStvoreni])
>>
>> The whole expression I tried was this:
>>
>> WHERE (DateDiff("d",[From date],[DatumStvoreni])>0)
>>
>> Tom Lake
>>
>>
>>

>
>



 
Reply With Quote
 
Petr Danes
Guest
Posts: n/a
 
      4th May 2010
Pretty sure - when I show the values without trying a compare, I don't see
anything wrong, and when I change the query to spin the DateDiff value out
to a table, the table contains only valid integers, positive and negative. I
can subsequently sort on that integer column and see that there is nothing
bad at either end. I can also select only the positive ones to get the
records I want, but creating an unnecessary temp table is an awkward way to
do it. The comparison should work.

Pete



"Douglas J. Steele" <NOSPAM_djsteele@NOSPAM_gmail.com> píše v diskusním
příspěvku news:(E-Mail Removed)...
> You sure you've got valid values for both fields in every row?
>
> --
> Doug Steele, Microsoft Access MVP
> http://I.Am/DougSteele
> (no private e-mails, please)
>
>
> "Petr Danes" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
>>I just tried the last query in another form, using a direct comparison
>>instead of the DateDiff function. Same error.
>>
>> SELECT Stvoreni_Convert.DatumStvoreni, CDate([DatumStvoreni]) AS Rozdil,
>> Stvoreni_Convert.AkcesPodrobnostiAutoID INTO STV
>> FROM Stvoreni_Convert
>> WHERE (((CDate([DatumStvoreni]))>CDate([From date])));
>>
>> Pete
>>
>>
>>
>> "Tom Lake" <(E-Mail Removed)> píse v diskusním príspevku
>> news:(E-Mail Removed)...
>>>
>>> "Petr Danes" <(E-Mail Removed)> wrote in message
>>> news:(E-Mail Removed)...
>>>> I have a set of queries that generate dates and want to use DateDiff to
>>>> select those records with certain dates are greater than others. The
>>>> following clause keeps giving me an incompatible data type error:
>>>>
>>>> WHERE DateDiff('d',[From date],[DatumStvoreni])>0
>>>>
>>>> I have the exact expression in the SELECT clause and it show positive
>>>> and negative integers just fine, but when I add this test, I get the
>>>> error. There are no bad values in the dataset, I've looked, and when I
>>>> export the DateDiff column to a temp table and run a query on that, it
>>>> works fine. Only when I use the test directly with the DateDiff
>>>> function does it bomb. I also tried putting CDate() around the field
>>>> expressions and it didn't help.
>>>
>>> I get an error when I use single quotes. I have to use this:
>>>
>>> DateDiff("d",[From date],[DatumStvoreni])
>>>
>>> The whole expression I tried was this:
>>>
>>> WHERE (DateDiff("d",[From date],[DatumStvoreni])>0)
>>>
>>> Tom Lake
>>>
>>>
>>>

>>
>>

>
>


 
Reply With Quote
 
Petr Danes
Guest
Posts: n/a
 
      6th May 2010
Never did find any answers to this, even when comparing dates directly in
the WHERE clause and avoiding the DateDiff function altogether, so I wound
up converting all my dates to strings, formatted as 'yyyy-mm-dd' and used
ordinary string comparisons to get what I needed.

Year-month-day is the only universally correct way to show dates anyway, but
it's pretty lame that dates can't be compared directly.

Pete


"Petr Danes" <(E-Mail Removed)> píše v diskusním příspěvku
news:(E-Mail Removed)...
>I have a set of queries that generate dates and want to use DateDiff to
>select those records with certain dates are greater than others. The
>following clause keeps giving me an incompatible data type error:
>
> WHERE DateDiff('d',[From date],[DatumStvoreni])>0
>
> I have the exact expression in the SELECT clause and it show positive and
> negative integers just fine, but when I add this test, I get the error.
> There are no bad values in the dataset, I've looked, and when I export the
> DateDiff column to a temp table and run a query on that, it works fine.
> Only when I use the test directly with the DateDiff function does it bomb.
> I also tried putting CDate() around the field expressions and it didn't
> help.
>
> Pete
>
>
>
> --
> This e-mail address is fake, to keep spammers and their address harvesters
> out of my hair. If you want to get in touch personally, I am 'pdanes' and
> I use yahoo mail. But please use the newsgroup when possible, so that all
> may benefit from the exchange of ideas.
>
>



 
Reply With Quote
 
New Member
Join Date: May 2011
Posts: 1
 
      17th May 2011
I'm experiencing the same problem when selecting from a view in sql2005 and cannot figure this out. However when i select into a temp table and then apply the filter to the query on the temp table it works....

This fails:
select * from View where DATEDIFF(day, CreateDate, '20110512') = 0

This works:
SELECT * into #mytemp FROM View

select * from #mytemp where DATEDIFF(day, CreateDate, '20110512') = 0
 
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
DateDiff function =?Utf-8?B?bWFyZ290X2Vvbg==?= Microsoft Access Form Coding 2 29th Jan 2010 03:07 PM
DateDiff function RL Microsoft Access Queries 3 18th Jul 2008 04:43 PM
datediff function compile error =?Utf-8?B?TWljaGFlbCBXaGl0bmV5?= Microsoft Access Queries 3 13th Jul 2007 10:07 PM
Datediff Function Denise Microsoft Access 2 10th Jul 2004 05:11 AM
Q: throwing record to a function Mark Microsoft Access Queries 5 7th Nov 2003 08:04 PM


Features
 

Advertising
 

Newsgroups
 


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