dbTalk Databases Forums  

Trouble using SYSDATE()

comp.databases.mysql comp.databases.mysql


Discuss Trouble using SYSDATE() in the comp.databases.mysql forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
b.coolsaet@gmail.com
 
Posts: n/a

Default Trouble using SYSDATE() - 01-03-2007 , 05:06 AM






Hi,

I have to select elements that are not older than 2 months (=60 days).
The table has a standard DATE column with format (YYYY-MM-DD), called
'date'.

I tried a lot of queries, one of them is following:

SELECT * FROM mytable WHERE date > (SYSDATE()-60);

So basically i'm telling mysql to select elements where the date is
bigger (= older) than the actual system date diminished with 60 days.
But it does't return anything (and yes I have elements newer than 2
months in the table :-)

I suppose something is wrong with the use of SYSDATE. Anyone any
experience with this ?


Reply With Quote
  #2  
Old   
Captain Paralytic
 
Posts: n/a

Default Re: Trouble using SYSDATE() - 01-03-2007 , 06:12 AM







b.coolsaet (AT) gmail (DOT) com wrote:

Quote:
Hi,

I have to select elements that are not older than 2 months (=60 days).
The table has a standard DATE column with format (YYYY-MM-DD), called
'date'.

I tried a lot of queries, one of them is following:

SELECT * FROM mytable WHERE date > (SYSDATE()-60);

So basically i'm telling mysql to select elements where the date is
bigger (= older) than the actual system date diminished with 60 days.
But it does't return anything (and yes I have elements newer than 2
months in the table :-)

I suppose something is wrong with the use of SYSDATE. Anyone any
experience with this ?
The user manual has a lot of experience of this and explains how to do
it.

Think about it. SYSDATE() "Returns the current date and time as a value
in 'YYYY-MM-DD HH:MM:SS' or YYYYMMDDHHMMSS format, depending on whether
the function is used in a string or numeric context." (quoted from
manual)

If you simply substract 60 from this, how is MySQL to know 60 what?
60 years?
60 seconds?
60 bars of chocolate?

Try:
SELECT * FROM mytable WHERE date > (SYSDATE()-INTERVAL 60 DAY);

as the manual suggests
http://dev.mysql.com/doc/refman/5.0/...functions.html



Reply With Quote
  #3  
Old   
Sean
 
Posts: n/a

Default Re: Trouble using SYSDATE() - 01-03-2007 , 07:32 AM



I think that it would be better with

SELECT * FROM mytable WHERE date > adddate(sysdate(), -60)

Which is a spin off from "date_add(date, -60, DAY)"

You can swap DAY for anything like:

MICROSECOND
SECOND
MINUTE
HOUR
DAY
WEEK
MONTH
QUARTER
YEAR


Hope that helps.


"Captain Paralytic" <paul_lautman (AT) yahoo (DOT) com> wrote

Quote:
b.coolsaet (AT) gmail (DOT) com wrote:

Hi,

I have to select elements that are not older than 2 months (=60 days).
The table has a standard DATE column with format (YYYY-MM-DD), called
'date'.

I tried a lot of queries, one of them is following:

SELECT * FROM mytable WHERE date > (SYSDATE()-60);

So basically i'm telling mysql to select elements where the date is
bigger (= older) than the actual system date diminished with 60 days.
But it does't return anything (and yes I have elements newer than 2
months in the table :-)

I suppose something is wrong with the use of SYSDATE. Anyone any
experience with this ?

The user manual has a lot of experience of this and explains how to do
it.

Think about it. SYSDATE() "Returns the current date and time as a value
in 'YYYY-MM-DD HH:MM:SS' or YYYYMMDDHHMMSS format, depending on whether
the function is used in a string or numeric context." (quoted from
manual)

If you simply substract 60 from this, how is MySQL to know 60 what?
60 years?
60 seconds?
60 bars of chocolate?

Try:
SELECT * FROM mytable WHERE date > (SYSDATE()-INTERVAL 60 DAY);

as the manual suggests
http://dev.mysql.com/doc/refman/5.0/...functions.html





Reply With Quote
  #4  
Old   
Captain Paralytic
 
Posts: n/a

Default Re: Trouble using SYSDATE() - 01-03-2007 , 08:00 AM




Sean wrote:

Quote:
I think that it would be better with

SELECT * FROM mytable WHERE date > adddate(sysdate(), -60)

Which is a spin off from "date_add(date, -60, DAY)"

You can swap DAY for anything like:

MICROSECOND
SECOND
MINUTE
HOUR
DAY
WEEK
MONTH
QUARTER
YEAR


Hope that helps.

Why would it be better?
And why wouldn't you use subdate rather than adddate with a negative
argument?



Reply With Quote
  #5  
Old   
Sean
 
Posts: n/a

Default Re: Trouble using SYSDATE() - 01-04-2007 , 03:14 AM




"Captain Paralytic" <paul_lautman (AT) yahoo (DOT) com> wrote

Quote:
Sean wrote:

I think that it would be better with

SELECT * FROM mytable WHERE date > adddate(sysdate(), -60)

Which is a spin off from "date_add(date, -60, DAY)"

You can swap DAY for anything like:

MICROSECOND
SECOND
MINUTE
HOUR
DAY
WEEK
MONTH
QUARTER
YEAR


Hope that helps.

Why would it be better?
And why wouldn't you use subdate rather than adddate with a negative
argument?

Sorry, you're right.

I was implying that this would be a better option than SYSDATE()-INTERVAL 60
DAY and overlooked "sub_date", which of course makes even more sense.

SA





Reply With Quote
  #6  
Old   
Captain Paralytic
 
Posts: n/a

Default Re: Trouble using SYSDATE() - 01-04-2007 , 04:02 AM




Sean wrote:

Quote:
"Captain Paralytic" <paul_lautman (AT) yahoo (DOT) com> wrote in message
news:1167832817.316730.194960 (AT) s34g2000cwa (DOT) googlegroups.com...

Sean wrote:

I think that it would be better with

SELECT * FROM mytable WHERE date > adddate(sysdate(), -60)

Which is a spin off from "date_add(date, -60, DAY)"

You can swap DAY for anything like:

MICROSECOND
SECOND
MINUTE
HOUR
DAY
WEEK
MONTH
QUARTER
YEAR


Hope that helps.

Why would it be better?
And why wouldn't you use subdate rather than adddate with a negative
argument?


Sorry, you're right.

I was implying that this would be a better option than SYSDATE()-INTERVAL 60
DAY and overlooked "sub_date", which of course makes even more sense.

SA
But why is subdate (or date_sub) better than the opure maths approach
of
SYSDATE()-INTERVAL 60 ?



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



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.