Web Wiz - Green Windows Web Hosting

  New Posts New Posts RSS Feed - Match year part of a date
  FAQ FAQ  Forum Search   Events   Register Register  Login Login

Match year part of a date

 Post Reply Post Reply Page  12>
Author
pedalcars View Drop Down
Senior Member
Senior Member


Joined: 12 August 2002
Location: United Kingdom
Status: Offline
Points: 268
Post Options Post Options   Thanks (0) Thanks(0)   Quote pedalcars Quote  Post ReplyReply Direct Link To This Post 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
www.pedalcars.info

The most fun on four wheels

Back to Top
Gary View Drop Down
Senior Member
Senior Member
Avatar

Joined: 20 March 2002
Location: United Kingdom
Status: Offline
Points: 326
Post Options Post Options   Thanks (0) Thanks(0)   Quote Gary Quote  Post ReplyReply Direct Link To This Post Posted: 28 March 2003 at 2:20am
strSQL = SELECT tblTable.* WHERE (YEAR)tblTable.Date = " & lngYear & ";"
Back to Top
MorningZ View Drop Down
Senior Member
Senior Member
Avatar

Joined: 06 May 2002
Location: United States
Status: Offline
Points: 1793
Post Options Post Options   Thanks (0) Thanks(0)   Quote MorningZ Quote  Post ReplyReply Direct Link To This Post 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
Back to Top
pedalcars View Drop Down
Senior Member
Senior Member


Joined: 12 August 2002
Location: United Kingdom
Status: Offline
Points: 268
Post Options Post Options   Thanks (0) Thanks(0)   Quote pedalcars Quote  Post ReplyReply Direct Link To This Post 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!
www.pedalcars.info

The most fun on four wheels

Back to Top
pedalcars View Drop Down
Senior Member
Senior Member


Joined: 12 August 2002
Location: United Kingdom
Status: Offline
Points: 268
Post Options Post Options   Thanks (0) Thanks(0)   Quote pedalcars Quote  Post ReplyReply Direct Link To This Post 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
www.pedalcars.info

The most fun on four wheels

Back to Top
MorningZ View Drop Down
Senior Member
Senior Member
Avatar

Joined: 06 May 2002
Location: United States
Status: Offline
Points: 1793
Post Options Post Options   Thanks (0) Thanks(0)   Quote MorningZ Quote  Post ReplyReply Direct Link To This Post 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
Back to Top
pedalcars View Drop Down
Senior Member
Senior Member


Joined: 12 August 2002
Location: United Kingdom
Status: Offline
Points: 268
Post Options Post Options   Thanks (0) Thanks(0)   Quote pedalcars Quote  Post ReplyReply Direct Link To This Post 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...
www.pedalcars.info

The most fun on four wheels

Back to Top
michael View Drop Down
Senior Member
Senior Member
Avatar

Joined: 08 April 2002
Location: United States
Status: Offline
Points: 4670
Post Options Post Options   Thanks (0) Thanks(0)   Quote michael Quote  Post ReplyReply Direct Link To This Post 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
Back to Top
 Post Reply Post Reply Page  12>

Forum Jump Forum Permissions View Drop Down

Forum Software by Web Wiz Forums® version 12.08
Copyright ©2001-2026 Web Wiz Ltd.


Become a Fan on Facebook Follow us on X Connect with us on LinkedIn Web Wiz Blogs
About Web Wiz | Contact Web Wiz | Terms & Conditions | Cookies | Privacy Notice

Web Wiz is the trading name of Web Wiz Ltd. Company registration No. 05977755. Registered in England and Wales.
Registered office: Web Wiz Ltd, Unit 18, The Glenmore Centre, Fancy Road, Poole, Dorset, BH12 4FB, UK.

Prices exclude VAT at 20% unless otherwise stated. VAT No. GB988999105 - $, € prices shown as a guideline only.

Copyright ©2001-2026 Web Wiz Ltd. All rights reserved.