| Author |
Topic Search Topic Options
|
pedalcars
Senior Member
Joined: 12 August 2002
Location: United Kingdom
Status: Offline
Points: 268
|
Post Options
Thanks(0)
Quote Reply
Topic: Match year part of a date Posted: 27 March 2003 at 3:13pm |
|
Following on from an earlier enquiry, is it possible to match part of a date field in an SQL query?
EG: If I have lngYear = 2003, can I have:
strSQL = SELECT tblTable.* WHERE (the year part of)tblTable.Date = " & lngYear & ";"
??
Failing that, it wouldn't be much trouble for me to use (eg) 1/1/2003 (instead of simply 2003).
I would then need to SELECT WHERE tblTable.Date was between 1/1/2003 and 1/1/2003 plus one year, which is something else I don't know how to do!
Ideally I'd like to use the first option though, if possible.
Ta
|
|
|
 |
Gary
Senior Member
Joined: 20 March 2002
Location: United Kingdom
Status: Offline
Points: 326
|
Post Options
Thanks(0)
Quote Reply
Posted: 28 March 2003 at 2:20am |
|
strSQL = SELECT tblTable.* WHERE (YEAR)tblTable.Date = " & lngYear & ";"
|
 |
MorningZ
Senior Member
Joined: 06 May 2002
Location: United States
Status: Offline
Points: 1793
|
Post Options
Thanks(0)
Quote Reply
Posted: 28 March 2003 at 5:22am |
|
actually
strSQL = "SELECT * WHERE SELECT DATEPART( year, [Date] ) = " & lngYear
Also note, its baaaaaaaaaaaaaaaaad practice to use reserved words as column names, you are just asking for trouble.... in the above example, there's [ ]'s to combat this, but its still bad practice
Edited by MorningZ
|
|
Contribute to the working anarchy we fondly call the Internet
|
 |
pedalcars
Senior Member
Joined: 12 August 2002
Location: United Kingdom
Status: Offline
Points: 268
|
Post Options
Thanks(0)
Quote Reply
Posted: 01 April 2003 at 4:48am |
|
Thanks MorningZ, that would explain why it wasn't working to start with!
As it happens the column isn't actually called "Date" (it's "Start_Date" - is that OK?); I just abbreviated for simplicity of typing. The table isn't called tblTable, either!
|
|
|
 |
pedalcars
Senior Member
Joined: 12 August 2002
Location: United Kingdom
Status: Offline
Points: 268
|
Post Options
Thanks(0)
Quote Reply
Posted: 01 April 2003 at 5:10am |
|
Hmm. Still got a problem.
Here's the code (not the full statement I want to use, but an initial, simple one, to test the theory):
strSQL = "SELECT tblDates.Start_Date FROM tblDates WHERE SELECT DATEPART(year,tblDates.Start_Date) = " & lngYear & ";"
And here's the error...
Microsoft OLE DB Provider for ODBC Drivers error '80040e14'
[Microsoft][ODBC Microsoft Access Driver] Syntax error. in query expression 'SELECT DATEPART(year,tblDates.Start_Date) = 2003'.
I'm guessing the syntax bit is the second use of "SELECT". Taking it out gives, instead:
Microsoft OLE DB Provider for ODBC Drivers error '80040e10'
[Microsoft][ODBC Microsoft Access Driver] Too few parameters. Expected 1.
lngYear exists (I've response.writen the strSQL and it shows as strSQL = SELECT tblDates.Start_Date FROM tblDates WHERE DATEPART(year,tblDates.Start_Date) = 2003;
Any more ideas?
Ta
|
|
|
 |
MorningZ
Senior Member
Joined: 06 May 2002
Location: United States
Status: Offline
Points: 1793
|
Post Options
Thanks(0)
Quote Reply
Posted: 01 April 2003 at 5:17am |
|
well yeah, the first error told you that "SELECT" didn't belong... (my bad on the typo, unfortunately there's no "validate SQL/Script" on this forum
as for the second error, i tried the following and it worked perfect
SELECT * FROM Trltbl WHERE DatePart( year, SignDate ) = 2003
|
|
Contribute to the working anarchy we fondly call the Internet
|
 |
pedalcars
Senior Member
Joined: 12 August 2002
Location: United Kingdom
Status: Offline
Points: 268
|
Post Options
Thanks(0)
Quote Reply
Posted: 01 April 2003 at 5:32am |
|
No, I don't understand why it's not working either! I have replace the lngYear bit with a number as per your example (tried 2002 and 2003 which are both present in strat_Date) and it's still throwing a wobbly.
Going to take a break, restart, come back, kick it, and try agin...
|
|
|
 |
michael
Senior Member
Joined: 08 April 2002
Location: United States
Status: Offline
Points: 4670
|
Post Options
Thanks(0)
Quote Reply
Posted: 01 April 2003 at 8:16am |
|
Well if you are using access this would not work, actually much easier you could do: SELECT * FROM Trltbl WHERE year(SignDate) = 2003
|
|
|
 |