If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > ANSI SQL > SQL Query for DATE Field

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 02-10-04, 02:51
anuindia anuindia is offline
Registered User
 
Join Date: Oct 2003
Posts: 23
SQL Query for DATE Field

Hi,

I am using DB2 database and VB6 on Win XP. I need to select records from Database for some particular month. I am trying to extract YEAR from DATE field (named as INV_DATE) in database table INVOICE using the following code.


strqueryDate = "SELECT EXTRACT (YEAR from INV_DATE) from INVOICE where EXTRACT(YEAR from INV_DATE) = " + CStr(intYear) + ""

Set localrecordsetDate = AGFDB.CONAGF.Execute(strqueryDate)
totalRecCountDate = localrecordsetDate.RecordCount

this query runs well on DB2 database command window but while executing in code it gives me following error:

Run-time error '-2147217900(80040e14)':
[Microsoft][ODBC dBase Driver] Syntax error (missing operator) in query expression 'EXTRACT(YEAR from INV_DATE)'.


I am not able to understand what is wrong. Please help

Anurag
Reply With Quote
  #2 (permalink)  
Old 02-10-04, 03:07
vsriharsha vsriharsha is offline
Registered User
 
Join Date: Jan 2004
Location: India
Posts: 6
Arrow Try this....

Hello Anu,

Though I am no veteran in this subject, the following occured to my mind when I was parsing your query. But remember I use MS SQL Server 2000 (and you can estimate my knowledge in my thread that is just below yours

1) Extract function is not appropriate in that situation (atleast not in SQL Server). The function required is DATEPART( ). So, your query transforms to:

[code]
strqueryDate = "SELECT DATEPART(year,INV_DATE) from INVOICE where DATEPART(year,INV_DATE) = " + CStr(intYear) + ""
[code]

2) I guess the comparision is a text comparision and hence your CStr function's output should be in single quotes. Hence your query re-transforms to:

[code]
strqueryDate = "SELECT DATEPART(year,INV_DATE) from INVOICE where DATEPART(year,INV_DATE) = '" + CStr(intYear) + "'"
[code]

Thats all as far as I can see. But if it is wrong, dont crush your teeth in my name....look for a better answer from some veteran.

Cheers
-Harsha.
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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 Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On