Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how to return results where current date is before date stored field?

Status
Not open for further replies.

spewn

Programmer
May 7, 2001
1,034
hello.

i have a field in my database called cDateEnd. in there, i store a date, such as 6/20/2009.

i want to return results on all entries where the cDateEnd is after todays date.

this is what i'm trying to accomplish:

Code:
$sth = $dbh->prepare("select cID from mytable where cDateEnd IS LATER THAN TODAYS DATE");

if i have to pass a value for todays date, it will be formatted as 1/21/2009.

any ideas would be helpful.

thanks.

- g
 
something like:

Code:
 SELECT cID 
 FROM mytable 
 WHERE TO_DAYS(cDateEnd) > TODAYS(CURRENT_DATE)

Cheers,
Daddy

-----------------------------------------------------
What You See Is What You Get
Never underestimate tha powah of tha google!
 
i actually came up with this, and it seems to work:

Code:
$thisDate='2009-01-31';
$sth = $dbh->prepare("select cID from mytable where trim(DATE(cDateEnd)) > '$thisDate'");

this is good form, right?

i really appreciate the help, and this site.

thanks!

- g
 
agreed,

one remark:
Mysql already knows what time it is, so it's cheaper to use the built-in function, but hey, that's up to you!

[cheers]
/Daddy

-----------------------------------------------------
What You See Is What You Get
Never underestimate tha powah of tha google!
 
Spotted small error in the query, must be TO_DAYS() and not TODAYS()

Code:
SELECT cID 
 FROM mytable 
 WHERE TO_DAYS(cDateEnd) > TO_DAYS(CURRENT_DATE)

Now where is that darn Edit button?

[spin]

/Daddy

-----------------------------------------------------
What You See Is What You Get
Never underestimate tha powah of tha google!
 
why use the to_days function which is an unnecessary calculation. why not just compare the two dates?

WHERE cDateEnd > current_date()
 
yeah, correct indeed.

in fact I work a lot with timestamps which include the time part and then the TO_DAYS function is necessary to compare dates.

Cheers,
/Daddy

-----------------------------------------------------
What You See Is What You Get
Never underestimate tha powah of tha google!
 
you make it sound like you are forced to use TO_DAYS, but you aren't :)

whenever you apply a function to a column, the database optimizer cannot use an index on that column, so in many cases you end up with a table scan

the "best practice" method for choosing timestamp values for specific dates involves using an open-ended upper bound in a range test

for example, to get all rows for yesterday...
Code:
WHERE cDateEnd >= CURRENT_DATE - INTERVAL 1 DAY
  AND cDateEnd  < CURRENT_DATE
:)

r937.com | rudy.ca
Buy my new book Simply SQL from Amazon
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top