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 > Search by year

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 12-17-03, 09:55
Rob1982 Rob1982 is offline
Registered User
 
Join Date: Dec 2003
Posts: 22
Search by year

Hi,

I have created a table in sql plus, and one of my attributes is Date, this has a datatype of DATE.

I want to perfom a query on this attribute by just entering a year i.e. 2003. And returning all the items which have 03 at the end of their date.

Any ideas how to do this? As at the moment I can only enter in the exact date i.e. 21-MAR-03

Cheers
Reply With Quote
  #2 (permalink)  
Old 12-17-03, 10:30
gannet gannet is offline
Registered User
 
Join Date: Oct 2002
Location: Plymouth UK
Posts: 116
You can use TO_CHAR to return the year. In the WHERE clause you will need the following:

Code:
WHERE TO_CHAR (your_date_field, 'YY') = '03'

or 

WHERE TO_CHAR (your_date_field, 'YYYY') = '2003'
This will return all records with 2003 in your date field.
Reply With Quote
  #3 (permalink)  
Old 12-17-03, 11:25
Rob1982 Rob1982 is offline
Registered User
 
Join Date: Dec 2003
Posts: 22
Smile

Quote:
Originally posted by gannet
You can use TO_CHAR to return the year. In the WHERE clause you will need the following:

Code:
WHERE TO_CHAR (your_date_field, 'YY') = '03'

or 

WHERE TO_CHAR (your_date_field, 'YYYY') = '2003'
This will return all records with 2003 in your date field.
Thanks for the information
Reply With Quote
  #4 (permalink)  
Old 12-18-03, 07:25
andrewst andrewst is offline
Moderator.
 
Join Date: Sep 2002
Location: UK
Posts: 5,171
Another way would be:

WHERE datecol >= TO_DATE('01-JAN-2003','DD-MON-YYYY')
AND datecol < TO_DATE('01-JAN-2004','DD-MON-YYYY')

(I deliberately avoided using BETWEEN to avoid having to consider time of day).
__________________
Tony Andrews
http://tinyurl.com/tonyandrews
Reply With Quote
  #5 (permalink)  
Old 04-07-09, 14:17
dendalee dendalee is offline
Registered User
 
Join Date: Dec 2003
Posts: 137
Similar Question

I want to do the same thing as this post, except:

I have an unbound field which I enter the year I am searching for in the box. I want to see only those records which match the year.

I.e. if I have the following dates:

12/1/2008
3/31/2009
4/1/2009

I want to be able to enter 2009 in the unbound box on a separate form and see ONLY those records from 2009. Any suggestions?
Reply With Quote
  #6 (permalink)  
Old 04-07-09, 16:03
stolze stolze is offline
Registered User
 
Join Date: Jan 2007
Location: Jena, Germany
Posts: 2,662
Code:
SELECT ...
FROM   ...
WHERE  YEAR(date-column) = ?
Then bind the input to the parameter marker.
__________________
Knut Stolze
IBM DB2 Analytics Accelerator
IBM Germany Research & Development
Reply With Quote
  #7 (permalink)  
Old 04-08-09, 01:13
JarlH JarlH is offline
Registered User
 
Join Date: Dec 2008
Location: At work...
Posts: 68
And the ANSI SQL way to get the year part out of a date is to use EXTRACT, eg.
...WHERE EXTRACT(YEAR FROM datecolumn) = 2003


(However, just a few DBMS products support this "Intervals and datetime arithmetic" feature of the SQL standard.)
Reply With Quote
  #8 (permalink)  
Old 04-25-09, 07:04
Peter.Vanroose Peter.Vanroose is offline
Registered User
 
Join Date: Sep 2004
Location: Belgium
Posts: 1,079
Quote:
Originally Posted by gannet
You can use TO_CHAR to return the year:
WHERE TO_CHAR (your_date_field, 'YYYY') = '2003'
This is Oracle-specific. The SQL ANSI standard way is
Code:
WHERE EXTRACT(year FROM your_date_field) = 2003
This syntax is supported by (recent versions of) Oracle (and of any other standards-compliant database system), and should systematically replace and ban out any use of TO_CHAR, imho.
__________________
--_Peter Vanroose,
__IBM Certified Database Administrator, DB2 9 for z/OS
__IBM Certified Application Developer
__ABIS Training and Consulting
__http://www.abis.be/
Reply With Quote
  #9 (permalink)  
Old 04-25-09, 07:05
Peter.Vanroose Peter.Vanroose is offline
Registered User
 
Join Date: Sep 2004
Location: Belgium
Posts: 1,079
Quote:
Originally Posted by stolze
SELECT ...
FROM ...
WHERE YEAR(date-column) = ?
YEAR() is not standard SQL, and is not available in e.g. Oracle.
Also in DB2 and in SQLServer, forget about YEAR(...) and systematically start using EXTRACT(year FROM ...) !
__________________
--_Peter Vanroose,
__IBM Certified Database Administrator, DB2 9 for z/OS
__IBM Certified Application Developer
__ABIS Training and Consulting
__http://www.abis.be/
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