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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

DATE comparison issue 1

Status
Not open for further replies.

PaulFynch

IS-IT--Management
May 23, 2002
105
CA
Hello wonderful TekTip community,

I am stumped on this one, I need your help:

I have a field storing a string value of a year/month like this : 200802 in a field called 'display_month' that I need to compare to NOW()...when I run this query:

SELECT display_month,
YEAR(STR_TO_DATE(wd_orders_meta.display_month, '%Y%m')) AS display_year,
YEAR(NOW()) as now_year
FROM my_table
WHERE DATE_FORMAT(STR_TO_DATE(display_month, '%Y%m'), '%Y%m') = DATE_FORMAT(NOW(), '%Y%m')

My query runs and tells returns a record that has a display_month value of 200802 (next feb) essentially saying that 200802 = 200702. It doesn't do any other month, it just seems confused about the year. I tried using YEAR() for the comparison, but it still returns 2008 = 2007. I tried casting both values as DATE() in the comparison to no avail. I tried CAST() and convert with only errors in the comparison.

I'm stumped...anyone have any thoughts?
Thanks
Paul

"If you build it....it won't work the first time.....or the second ;)
 
Hey again,

I just checked the DB, the 'display_month' field is actually mediumint(9), if that makes any difference.

Paul

"If you build it....it won't work the first time.....or the second ;)
 
I'll give that a try r937 and let you know how it works.

pf

"If you build it....it won't work the first time.....or the second ;)
 
I found it r937, it had nothing to do with the date math (man I spent a couple hours on this one), a misplaced 'OR' (or rather, 'unbracketed') had my query accepting conditions that weren't relevant to the date...

Star for the effort...

Thanks,
Paul

"If you build it....it won't work the first time.....or the second ;)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top