Posted: 2/24/2009 10:45:04 AM EDT
|
Ok, this is really ticking me off. I just did this not a month ago and can't remember how I got it to work. I'm trying to search my database and find every row with a date later than x. Here's what I've got:
select en.first_name, en.last_name, en.suffix, en.street_address, en.city, en.state, en.zip_code, en.po_box from pubacc_en en, pubacc_hd hd where en.call_sign = hd.call_sign and DATEDIFF('01-01-2009', hd.grant_date) > 1 It took me a while the last time and I thought I saved the query but I thought wrong. When I run this I get no records back even though I know I have records later than the 1st of the year. Where'd I go wrong? |
|
Quoted:
Ok, this is really ticking me off. I just did this not a month ago and can't remember how I got it to work. I'm trying to search my database and find every row with a date later than x. Here's what I've got: select en.first_name, en.last_name, en.suffix, en.street_address, en.city, en.state, en.zip_code, en.po_box from pubacc_en en, pubacc_hd hd where en.call_sign = hd.call_sign and DATEDIFF('01-01-2009', hd.grant_date) > 1 It took me a while the last time and I thought I saved the query but I thought wrong. When I run this I get no records back even though I know I have records later than the 1st of the year. Where'd I go wrong? I don't use DATEDIFF a lot, but you can try specifying the first field fully. i.e. '01-01-2009 00:00:00'. That may do the trick. I usually do everything in epoch time, so I would use "where FROM_UNIXTIME('01-01-2009') > FROM_UNIXTIME(hd_grant_date)" Hope that helps. |
|
Quoted:
Quoted:
Ok, this is really ticking me off. I just did this not a month ago and can't remember how I got it to work. I'm trying to search my database and find every row with a date later than x. Here's what I've got: select en.first_name, en.last_name, en.suffix, en.street_address, en.city, en.state, en.zip_code, en.po_box from pubacc_en en, pubacc_hd hd where en.call_sign = hd.call_sign and DATEDIFF('01-01-2009', hd.grant_date) > 1 It took me a while the last time and I thought I saved the query but I thought wrong. When I run this I get no records back even though I know I have records later than the 1st of the year. Where'd I go wrong? I don't use DATEDIFF a lot, but you can try specifying the first field fully. i.e. '01-01-2009 00:00:00'. That may do the trick. I usually do everything in epoch time, so I would use "where FROM_UNIXTIME('01-01-2009') > FROM_UNIXTIME(hd_grant_date)" Hope that helps. |
|
select grant_date from hd;
check to make sure the date isnt yyyy-mm-dd (which I think it is) I have never used the datediff before. This site has always helped me out: java2s search for datediff. |
|
Try using the date in the comparison:
select en.first_name, en.last_name, en.suffix, en.street_address, en.city, en.state, en.zip_code, en.po_box from pubacc_en en, pubacc_hd hd where en.call_sign = hd.call_sign and hd.grant_date < '2009-01-01'; I think you were trying too hard ETA: Your column needs to be a datetime colum for this to work, if it is text then you will have to convert it first. |
|
Man this is driving me up the wall. I know it wasn't this painful the last time.
My problem is that the column is defined as a char(10). Unfortunately if I change it to a date type it clears every date in that column. I've tried calling STR_TO_DATE to convert the char to a date type that I can then use with DATEDIFF. That didn't work either. For example, if I query: select grant_date from pubacc_hd where call_sign='JaxShooter' it returns 09/08/2006. If I query: select STR_TO_DATE(grant_date, '%Y-%m-%d') from pubacc_hd where call_sign='JaxShooter' it returns an empty resultset. I've tried damned near every permutation, conversion, function that I can think of and can't figure it out. Damn this is pissing me off. You're probably right about trying too hard, though. I know I'm going to kick myself when it finally works. |
|
Quoted:
Man this is driving me up the wall. I know it wasn't this painful the last time. My problem is that the column is defined as a char(10). Unfortunately if I change it to a date type it clears every date in that column. I've tried calling STR_TO_DATE to convert the char to a date type that I can then use with DATEDIFF. That didn't work either. For example, if I query: select grant_date from pubacc_hd where call_sign='JaxShooter' it returns 09/08/2006. If I query: select STR_TO_DATE(grant_date, '%Y-%m-%d') from pubacc_hd where call_sign='JaxShooter' it returns an empty resultset. I've tried damned near every permutation, conversion, function that I can think of and can't figure it out. Damn this is pissing me off. You're probably right about trying too hard, though. I know I'm going to kick myself when it finally works. Your format string is wrong: select STR_TO_DATE(grant_date, '%m/%d/%Y') from pubacc_hd where call_sign='JaxShooter'; For example: select str_to_date('09/08/2006', '%m/%d/%Y'); +––––––––––––––––––––––––––––––––––––––-+ | str_to_date('09/08/2006', '%m/%d/%Y') | +––––––––––––––––––––––––––––––––––––––-+ | 2006-09-08 | +––––––––––––––––––––––––––––––––––––––-+ |
|
Damnit. I was trying to format it a different way. You, sir, have just won the prize. Thank you very much.
select en.first_name, en.last_name, en.suffix, en.street_address, en.city, en.state, en.zip_code, en.po_box, STR_TO_DATE(hd.grant_date, '%m/%d/%Y') 'Grant Date' from pubacc_en en, pubacc_hd hd where en.call_sign = hd.call_sign and DATEDIFF('2008-01-01', STR_TO_DATE(hd.grant_date, '%m/%d/%Y')) < 1 This works!!!!!
|
Win a FREE Membership!
Sign up for the ARFCOM weekly newsletter and be entered to win a free ARFCOM membership. One new winner* is announced every week!
You will receive an email every Friday morning featuring the latest chatter from the hottest topics, breaking news surrounding legislation, as well as exclusive deals only available to ARFCOM email subscribers.