Warning

 

Close
Confirm Action

Are you sure you wish to do this?

Cancel Confirm
AR15.COM
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?
2/24/2009 11:46:55 AM EDT
[#1]
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.
2/24/2009 11:48:14 AM EDT
[#2]
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.


Doh!  Just looked at it again.  It could be you've got your date screwed up.  Dates need to be YYYY-MM-DD, not MM-DD-YYYY.  I've been bit by that before ;-)
2/24/2009 12:10:54 PM EDT
[#3]
I tried the date the other way yesterday. Neither that nor FROM_UNIXTIME worked.

What really irks me is it was painfully simple once I realized what I was doing. Not sure why it's not clicking again.
2/24/2009 12:50:35 PM EDT
[#4]
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.

2/24/2009 1:08:53 PM EDT
[#5]
The dates come back as 03/16/2001 when I run just the select with grant_date.

I'll check out that site later tonight. Thx.
2/25/2009 10:16:00 AM EDT
[#6]
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.
2/25/2009 11:01:13 AM EDT
[#7]
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.
2/26/2009 10:13:12 AM EDT
[#8]
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                            |
+––––––––––––––––––––––––––––––––––––––-+
2/26/2009 10:38:55 AM EDT
[#9]
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!!!!!

2/26/2009 9:38:44 PM EDT
[#10]
Glad to help!

Close Join Our Mail List to Stay Up To Date! 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.


By signing up you agree to our User Agreement. *Must have a registered ARFCOM account to win.