Unix Technical Forum

find a date range between current date and 1 year ago

This is a discussion on find a date range between current date and 1 year ago within the MySQL forums, part of the Database Server Software category; --> hey i need to find a date range between the current date and the date one year ago based ...


Go Back   Unix Technical Forum > Database Server Software > MySQL

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 02-28-2008, 10:53 AM
Lord Kelvan
 
Posts: n/a
Default find a date range between current date and 1 year ago

hey i need to find a date range between the current date and the date
one year ago based on the current date

so far i have this sql code

SELECT type, SUM(quantity), date FROM table
WHERE date BETWEEN current_date() and ???????????????????????????
GROUP BY date, type
ORDER BY date;

i just need to know what i put in the ??????????????????????????? area

Regards
Kelvan

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 02-28-2008, 10:53 AM
J.O. Aho
 
Posts: n/a
Default Re: find a date range between current date and 1 year ago

Lord Kelvan wrote:
> hey i need to find a date range between the current date and the date
> one year ago based on the current date
>
> so far i have this sql code
>
> SELECT type, SUM(quantity), date FROM table
> WHERE date BETWEEN current_date() and ???????????????????????????
> GROUP BY date, type
> ORDER BY date;
>
> i just need to know what i put in the ??????????????????????????? area


One of the following should do what you need:

PERIOD_ADD(P,N)
ADDDATE(date,INTERVAL expr unit)
ADDDATE(expr,days)

http://dev.mysql.com/doc/refman/5.0/...ion_period-add

--

//Aho
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 02-28-2008, 10:53 AM
Lord Kelvan
 
Posts: n/a
Default Re: find a date range between current date and 1 year ago

On May 22, 4:19 pm, "J.O. Aho" <u...@example.net> wrote:
> One of the following should do what you need:
>
> PERIOD_ADD(P,N)
> ADDDATE(date,INTERVAL expr unit)
> ADDDATE(expr,days)
>
> http://dev.mysql.com/doc/refman/5.0/...nctions.html#f...
>
> --
>
> //Aho


thoes functions are good but they are for future dates not prior dates
i have the current date and basically i need a dynamic sql script to
take todays date and count back 1 year and get that date

regards
Kelvan

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 02-28-2008, 10:53 AM
J.O. Aho
 
Posts: n/a
Default Re: find a date range between current date and 1 year ago

Lord Kelvan wrote:
> On May 22, 4:19 pm, "J.O. Aho" <u...@example.net> wrote:
>> One of the following should do what you need:
>>
>> PERIOD_ADD(P,N)
>> ADDDATE(date,INTERVAL expr unit)
>> ADDDATE(expr,days)
>>
>> http://dev.mysql.com/doc/refman/5.0/...nctions.html#f...


> thoes functions are good but they are for future dates not prior dates
> i have the current date and basically i need a dynamic sql script to
> take todays date and count back 1 year and get that date


mysql> select ADDDATE(CURRENT_DATE(),INTERVAL -31 DAY);
+------------------------------------------+
| ADDDATE(CURRENT_DATE(),INTERVAL -31 DAY) |
+------------------------------------------+
| 2007-04-21 |
+------------------------------------------+
1 row in set (0.00 sec)

--

//Aho
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 02-28-2008, 10:53 AM
Captain Paralytic
 
Posts: n/a
Default Re: find a date range between current date and 1 year ago

On 22 May, 05:41, Lord Kelvan <the_idd...@hotmail.com> wrote:
> On May 22, 4:19 pm, "J.O. Aho" <u...@example.net> wrote:
>
> > One of the following should do what you need:

>
> > PERIOD_ADD(P,N)
> > ADDDATE(date,INTERVAL expr unit)
> > ADDDATE(expr,days)

>
> >http://dev.mysql.com/doc/refman/5.0/...nctions.html#f...

>
> > --

>
> > //Aho

>
> thoes functions are good but they are for future dates not prior dates
> i have the current date and basically i need a dynamic sql script to
> take todays date and count back 1 year and get that date
>
> regards
> Kelvan


so try DATE_SUB

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #6 (permalink)  
Old 02-28-2008, 10:54 AM
Lord Kelvan
 
Posts: n/a
Default Re: find a date range between current date and 1 year ago

On May 22, 8:30 pm, Captain Paralytic <paul_laut...@yahoo.com> wrote:
> On 22 May, 05:41, Lord Kelvan <the_idd...@hotmail.com> wrote:
>
>
>
>
>
> > On May 22, 4:19 pm, "J.O. Aho" <u...@example.net> wrote:

>
> > > One of the following should do what you need:

>
> > > PERIOD_ADD(P,N)
> > > ADDDATE(date,INTERVAL expr unit)
> > > ADDDATE(expr,days)

>
> > >http://dev.mysql.com/doc/refman/5.0/...nctions.html#f...

>
> > > --

>
> > > //Aho

>
> > thoes functions are good but they are for future dates not prior dates
> > i have the current date and basically i need a dynamic sql script to
> > take todays date and count back 1 year and get that date

>
> > regards
> > Kelvan

>
> so try DATE_SUB- Hide quoted text -
>
> - Show quoted text -


date sub wont work it is for subtracting dates but i realised one
night when i was trying to sleep that you should be able to use
negative numbers in the functions thank you for your help

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #7 (permalink)  
Old 02-28-2008, 10:54 AM
Captain Paralytic
 
Posts: n/a
Default Re: find a date range between current date and 1 year ago

On 23 May, 22:15, Lord Kelvan <the_idd...@hotmail.com> wrote:
> On May 22, 8:30 pm, Captain Paralytic <paul_laut...@yahoo.com> wrote:
>
>
>
>
>
> > On 22 May, 05:41, Lord Kelvan <the_idd...@hotmail.com> wrote:

>
> > > On May 22, 4:19 pm, "J.O. Aho" <u...@example.net> wrote:

>
> > > > One of the following should do what you need:

>
> > > > PERIOD_ADD(P,N)
> > > > ADDDATE(date,INTERVAL expr unit)
> > > > ADDDATE(expr,days)

>
> > > >http://dev.mysql.com/doc/refman/5.0/...nctions.html#f...

>
> > > > --

>
> > > > //Aho

>
> > > thoes functions are good but they are for future dates not prior dates
> > > i have the current date and basically i need a dynamic sql script to
> > > take todays date and count back 1 year and get that date

>
> > > regards
> > > Kelvan

>
> > so try DATE_SUB- Hide quoted text -

>
> > - Show quoted text -

>
> date sub wont work it is for subtracting dates but i realised one
> night when i was trying to sleep that you should be able to use
> negative numbers in the functions thank you for your help- Hide quoted text -
>
> - Show quoted text -


DATE_SUB will work as it will return a date in the past for you to use
in a compare!

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #8 (permalink)  
Old 02-28-2008, 10:54 AM
Lord Kelvan
 
Posts: n/a
Default Re: find a date range between current date and 1 year ago

yes and thats good but i want it to be dynamic so i dont have to touch
it periodadd with a negtive number will be good thx for ppls help

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #9 (permalink)  
Old 02-28-2008, 10:55 AM
Captain Paralytic
 
Posts: n/a
Default Re: find a date range between current date and 1 year ago

On 24 May, 23:07, Lord Kelvan <the_idd...@hotmail.com> wrote:
> yes and thats good but i want it to be dynamic so i dont have to touch
> it periodadd with a negtive number will be good thx for ppls help


DATE_SUB with a positive number is the equivalent of ADDDATE with a
negative number.

Both are as dynamic as each other.

PERIOD_ADD however does not return a date.

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 03:26 PM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0
www.UnixAdminTalk.com