473,748 Members | 2,461 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

SQL for finding years in dates?

Hello, I not sure if this is the right place to ask this... I am using
mysql.

What I need is a SQL statement that can find what years are in the
database that is greater than last year.

For example...

I have a table full of dates ranging from 2001 to 2005..

I am straining my brain to figure out a SQL statement that can give me
a list of years greater than last year..

In this case I need a list that has..

2003
2004
2005

If anyone could help me.. or give me a clue.. I would appreciate it.
Jul 19 '05 #1
2 26605
I finally did figure out how to do this.. I post my solution just in
case somebody else finds it useful or has a suggestion for improvment.

SELECT YEAR(startdate) AS year
FROM course_dates
WHERE YEAR(startdate) > (YEAR(NOW())-1)
GROUP BY YEAR(startdate)

Aggro <sp**********@y ahoo.com> wrote in message news:<AC******* *****@read3.ine t.fi>...
Jason Tudisco wrote:
Hello, I not sure if this is the right place to ask this... I am using
mysql.

What I need is a SQL statement that can find what years are in the
database that is greater than last year.

For example...

I have a table full of dates ranging from 2001 to 2005..

I am straining my brain to figure out a SQL statement that can give me
a list of years greater than last year..

In this case I need a list that has..

2003
2004
2005

If anyone could help me.. or give me a clue.. I would appreciate it.


Use mysql function year(date) to extract year from date, and compare
years. For example

--------------------------------------------------
mysql> create table datetest(date_ date);
Query OK, 0 rows affected (0.00 sec)

mysql> insert into datetest values( '2002-01-01' );
Query OK, 1 row affected (0.01 sec)

mysql> insert into datetest values( '2003-01-01' );
Query OK, 1 row affected (0.01 sec)

mysql> insert into datetest values( '2004-01-01' );
Query OK, 1 row affected (0.00 sec)

mysql> insert into datetest values( '2005-01-01' );
Query OK, 1 row affected (0.00 sec)

mysql> select * from datetest where year(date_) > (year(now())-1);
+------------+
| date_ |
+------------+
| 2003-01-01 |
| 2004-01-01 |
| 2005-01-01 |
+------------+
3 rows in set (0.03 sec)
--------------------------------------------------

Read more about date and time functions from:

http://www.mysql.com/doc/en/Date_and...functions.html

Jul 19 '05 #2
I finally did figure out how to do this.. I post my solution just in
case somebody else finds it useful or has a suggestion for improvment.

SELECT YEAR(startdate) AS year
FROM course_dates
WHERE YEAR(startdate) > (YEAR(NOW())-1)
GROUP BY YEAR(startdate)

Aggro <sp**********@y ahoo.com> wrote in message news:<AC******* *****@read3.ine t.fi>...
Jason Tudisco wrote:
Hello, I not sure if this is the right place to ask this... I am using
mysql.

What I need is a SQL statement that can find what years are in the
database that is greater than last year.

For example...

I have a table full of dates ranging from 2001 to 2005..

I am straining my brain to figure out a SQL statement that can give me
a list of years greater than last year..

In this case I need a list that has..

2003
2004
2005

If anyone could help me.. or give me a clue.. I would appreciate it.


Use mysql function year(date) to extract year from date, and compare
years. For example

--------------------------------------------------
mysql> create table datetest(date_ date);
Query OK, 0 rows affected (0.00 sec)

mysql> insert into datetest values( '2002-01-01' );
Query OK, 1 row affected (0.01 sec)

mysql> insert into datetest values( '2003-01-01' );
Query OK, 1 row affected (0.01 sec)

mysql> insert into datetest values( '2004-01-01' );
Query OK, 1 row affected (0.00 sec)

mysql> insert into datetest values( '2005-01-01' );
Query OK, 1 row affected (0.00 sec)

mysql> select * from datetest where year(date_) > (year(now())-1);
+------------+
| date_ |
+------------+
| 2003-01-01 |
| 2004-01-01 |
| 2005-01-01 |
+------------+
3 rows in set (0.03 sec)
--------------------------------------------------

Read more about date and time functions from:

http://www.mysql.com/doc/en/Date_and...functions.html

Jul 19 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
5390
by: Richard Hollenbeck | last post by:
I'm trying to write some code that will convert any of the most popular standard date formats twice in to something like "dd Mmm yyyy" (i.e. 08 Jan 1908) and compare the first with the second and calculate days, months, and years. This is not for a college course. It's for my own personal genealogy website. I'm stumped about the code. I'm working on it but not making much progress. Is there any free code available anywhere? I know it...
7
12813
by: Bambero | last post by:
Hello all Problem like in subject. There is no problem when I want to count days between two dates. Problem is when I want to count years becouse of leap years. For ex. between 2002-11-19 2003-11-19
9
3941
by: Rich | last post by:
Thanks for the Help in my previous post. I've been working on this and it's almost what I want. I want to obtain the user's current age by comparing their date of birth (user inputs) to the current date. I know how to get the Current Date but I'm not finding how to calculate the Current Date minus the User's Birthday.
22
4164
by: mike | last post by:
If I had a date in the format "01-Jan-05" it does not sort properly with my sort routine: function compareDate(a,b) { var date_a = new Date(a); var date_b = new Date(b); if (date_a < date_b) { return -1; } else
6
27989
by: carl.barrett | last post by:
Hi, I have a continuous form based on a query ( I will also be creating a report based on the same query). There are 2 fields: Date Obtained and Date Of Expiry I want a further 3 columns to the right of these 2 fields to show the
4
7783
by: Russell | last post by:
I have an assignment that I have to complete. I have to write a windows app in C#. Here is the spec: 1/ Date Comparison Build a program that will find the number of days between two dates. You CANNOT use the inbuilt date functions or the DateTime Type. Your program should work with any date between 1900 and 3000. Here are your acceptance tests:
4
15751
by: jamesyreid | last post by:
Hi, I'm really sorry to post this as I know it must have been asked countless times before, but I can't find an answer anywhere. Does anyone have a snippet of JavaScript code I could borrow which calculated the difference in years and days between two dates, and takes leap years into account? I'm calculating the difference in the usual way, i.e....
23
14055
by: thebjorn | last post by:
For the purpose of finding someone's age I was looking for a way to find how the difference in years between two dates, so I could do something like: age = (date.today() - born).year but that didn't work (the timedelta class doesn't have a year accessor). I looked in the docs and the cookbook, but I couldn't find anything, so
6
1680
by: BonnieW via AccessMonster.com | last post by:
I inherited a table. One field in this table is Date_Observed, and users entered data directly into this (I'm not sure there was an input mask; if there was, it was ignored). It is formatted as a Date/Time field. In it, I have such gems as "2/1/530" and "1/1/999". I have ruled out the possibility that my users are time-travellers, but I still have to figure out when these things were observed. Is there a formula out there that'll help...
0
8995
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9558
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9378
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9331
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9253
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6798
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4608
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3316
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2216
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.