i want to compare database field of date that has YYYY-MM-DD
format and i want to compare from MM-DD
actually i want to fetch records those have birthdays between month /selected week durations
and birthdate is stores as YYYY-MM-DD format in db table
how can i achieve this kindly help.
Look at this:
http://dev.mysql.com/doc/refman/5.6/en/date-and-time-functions.html
you can use CURDATE(), DATE_ADD() and DATE_SUB() to make date interval
Within your sql you could try something like the following:
select * from people where birthdate>='10/01/2010' AND birthdate<='30/01/2010';
if you want to get more specific with months and days then you can utilise the month(birthdate) and day(birthdate) functions in mysql;
select * from people where month(birthdate)=10;
would return all your people where the month for birthdate is October.
Related
Hi there please help me if you can. Here is my senario:
I have a MySQL database with a column that holds a date in the form of a varchar. The format of the date is the following 29/05/2014 (i.e. d/m/Y).
I'm trying to compare the value of this column with todays date and return any rows where the date is earlier than todays date.
I'm using a php variable to store todays as follows:
$date = date("d/m/Y");
Here is my SQL query:
SELECT * FROM patients WHERE last_seen < '$date'
What gets returned
So what is returned is very unusual (to me). All records where the last_seen "day" is less than todays "day". It seems to be overlooking the month and year. So in other words if I last_seen = "30/05/2014" and todays date is "29/05/2014" this record is still returned.
Does anyone have any ideas what I might be doing wrong here?
Thanks
You really, really shouldn't store dates in a varchar field - use date or datetime or timestamp data type.
That said, sometimes you don't have control over the database and you have to deal with somebody else's bad design decision. In this case, to compare dates, convert the varchar strings to dates and compare them that way. So, in your case, you can have something like this:
$date = date("d/m/Y");
and then
SELECT * FROM patients WHERE str_to_date('last_seen', '%d/%m/%Y') < str_to_date('$date', '%d/%m/%Y')
or simpler
SELECT * FROM patients WHERE date(last_seen) < current_date
This way you are actually comparing dates and not strings containing dates. Naturally, this assumes that all dates are stored in the same format.
EDIT: I just tested the last option - and, apparently, date('30/05/2014') returns NULL on my system (mysql 5.5 on linux), hence I suggest the best way is
SELECT * FROM patients WHERE str_to_date('last_seen', '%d/%m/%Y') < current_date
You need to store your date as DATE or DATETIME in your database.
Then you can use:
SELECT * FROM patients WHERE DATE(last_seen) < CURRENT_DATE
Evrey row in my table has a date Y-m-d. Now I want to select only those rows that have a date from before a certain month and year independet of the day. So for example I want all rows with a date befor january 2014.
How would I do this in MySQL? Only considering the month does not work, because it will give me also rows with a month smaller, but a year after my year. E.g. i want rows with date smaller than 03/2013. It will give me also 02/2014.
Of course I could in php define first the first day of the month and than compare the full date. But this does no seem too great and when I want to compare dates after a certain month I have to know how many days a month has.
You can use the STR_TO_DATE function to convert to actual DATE type and then do a simple comparison to the cut-off date.
SELECT *
FROM Your_Table
WHERE STR_TO_DATE(Your_Date_Column,'%Y-%m-%d') < STR_TO_DATE('2013-03-01','%Y-%m-%d');
I have a UNIX timestamp in a table like 1321521158 and want to get the details from a table for the particular date, like:
$mydate="11/11/2011";
SELECT notes FROM table WHERE `addeddate`=$mydate
How do I get it?
you can use the mysql date function like:
WHERE date(addedate) = date(mydate)
or you can calculate the starting and ending timestamp for the given day you want to know and then do
WHERE addeddate >= mystarttimestamp AND addeddate <= myendtimestamp
try
WHERE $mydate = FROM_UNIXTIME('dateColumn')'%Y/%D/%M');
MySQL Date Time Functions
I want to insert date in format m-d into database. I used this code
$date1=date("m-d",mktime(0,0,0,$month1 ,$day1,1));
The date is inserted as 0000-00-00 in MySQL, and I don't know why.
I want to do that to compare that date stored in MySQL with current month and day. How can this be done?
mysql date must be of the form Y-m-d . Building a date with m-d form brings an invalid date.
If you want to compare only month or day, you can use mysql MONTH and DAYOFMONTH functions. For example:
$query = "SELECT * FROM table WHERE MONTH(datecol) = {$month1} AND DAYOFMONTH(datecol) = {$day1}"
$date1=date("Y-m-d",mktime(0,0,0,$month1 ,$day1,1));
I am trying to select all records in a table which have a date between the current date and 1 month ahead.
The date is stored like this DD-MM-YYYY
And the query I have tried:
SELECT * from tablename WHERE renewalDate BETWEEN DATE_FORMAT(CURDATE(),GET_FORMAT(DATE,'EUR')) AND DATE_ADD(DATE_FORMAT(CURDATE(),GET_FORMAT(DATE,'EUR')), INTERVAL 1 MONTH)
But this does not return the correct results.
Is the date stored in an actual date or datetime field? If it's in a char/varchar field, you won't be able to use the BETWEEN syntax, as mysql will just treat them as fixed strings.