I have stored in the database in a varchar column the birthdays like this
dd/mm/YYYY
How can I select the birthday people from the current month directly from MySQL query??
And show using PHP
Thanks
First, do not store dates as a VARCHAR. Convert it to a DATE.
Once that's fixed, use one of the many MySQL date time functions:
SELECT * FROM users WHERE MONTH(birthday) = MONTH(NOW());
SELECT
*
FROM
yourtable
WHERE
MONTH(STR_TO_DATE(yourdatefield, '%d/%m/%Y')) = MONTH(NOW())
Assuming date is stored in %m/%d/%Y this format you can change this format according to your need.
and %m we are selecting only the month and comparing it to the current month MONTH(NOW()).
Replace DOB by your column and table by your table name
select * from table
where date_format(str_to_date(DOB, '%m/%d/%Y'), '%m') = MONTH(NOW());;
You should change your column type to DATE. e.g.
ALTER TABLE `people` CHANGE `dob` `dob` DATE NOT NULL;
By doing so you can then use the MySQL query date functions to filter the results.
SELECT * FROM people WHERE MONTH(dob) = MONTH(CURDATE()) AND YEAR(dob) = YEAR(CURDATE())
For Get User list whose birthday in current month in mysql if field datatype is date
$Toaday = '%'.date('-m-').'%';
$query = " select * from client where birth_date LIKE '$Toaday' ";
In your case declare $Today = '%'.date('/m/').'%';
As you used a VARCHAR field instead a DATE field,
you have to cast the value and then use the normal date functions. like
SELECT *
FROM table_name
WHERE MONTH(CAST(col_name AS DATE)) = MONTH(NOW());
I have store dates as a timestamp in tables so I create a query like this. and it's working fine.
don't use varchar for a date.
select first_name, last_name, date_format(FROM_UNIXTIME(`dateofbirth`), '%m/%d/%Y') as dob from users where date_format(FROM_UNIXTIME(`dateofbirth`), '%m') = MONTH(NOW())
Related
I have a table that has dates stored as text and not as a date_time. I want to select rows that happened today. I can hard code the date, but obvious I want to do it dynamically.
SELECT * FROM view WHERE anniversary LIKE '%09-07'
SELECT * FROM view WHERE anniversary LIKE '%' + DATE_FORMAT(now(), "%m-%d")
First, you should be storing dates as dates, not as strings.
But, if they are stored as strings, you can use a comparison like this:
where right(anniversary_datestring, 5) = str_to_date(curdate(), '%m-%d')
But, I recommend fixing your data. If the values are in the format YYYY-MM-DD, simply do:
alter table t modify column anniversary date;
The month and day operators can be used:
SELECT * from view WHERE MONTH(anniversary) = MONTH(NOW()) AND DAY(anniversary) = DAY(NOW())
if the column is of type varchar the STR_TO_DATE function can be used to turn it into a date:
SELECT * from view WHERE MONTH(STR_TO_DATE(anniversary,'%Y-%m-%d')) = MONTH(NOW()) AND DAY(STR_TO_DATE(anniversary,'%Y-%m-%d')) = DAY(NOW())
This should work,
convert string to date and check in where clause
SELECT * FROM view WHERE STR_TO_DATE(anniversary, '%d-%m-%Y') = CURDATE()
I Have one column in the table called "date". In this column there are some dates stored from different months. I want to display only those dates whose month is '10'. How can we write query to print the desired output using substring() function of mysql ? Or else any other solution for this ?
O/p : first 8 records from this column.
MySQL has a lot of options for working with dates. In this case, combining the MONTH() function with STR_TO_DATE() would be easiest; MONTH() takes a date as input and returns the month number, starting at 1 for January, while STR_TO_DATE() will format your (non-standard) date string into a date MySQL understands.
Your query would then become:
SELECT * FROM your_table WHERE MONTH(STR_TO_DATE(`date`, '%d-%m-%Y')) = 10;
select * from your_table where month(date) = 10;
Use DATE_FORMAT then DATE_FORMAT(date, '%m') to be able to get the value of month alone
SELECT * FROM tbl WHERE DATE_FORMAT(date, '%m') = '10'
SELECT `date` FROM table_name
WHERE MONTH(`date`) = '10'
ORDER BY `date` -- without this, the a random 8 would be delivered
LIMIT 8
If date column is datetime or Time stamp then
SELECT `date` FROM `tablename` WHERE month(`date`) =10
I would like to ask if my Mysql statement is correct or not.. When I run this under mysql it does not return any error but I cannot retrieve the row for it. Here's my statement:
SELECT * FROM timekeeping WHERE createddate = NOW()
Here's what my table looks like
MySQL compare now() (only date, not time) with a datetime field
Try this:
SELECT * FROM timekeeping WHERE DATE(createddate) = DATE(NOW());
Most likely the createddate = NOW() is an exact time comparison , you are probably only interested in the year, month, day being the the same.
See here for details on how to do what you are trying to do:
http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html
Probably you want to search for today date.
Try this:
SELECT * FROM timekeeping WHERE DATE(createddate) = DATE(NOW());
now() includes the time. Given that your fields contain date AND time values, you'll only ever get a match if the date AND time are exact matches. You need to compare dates only:
... WHERE DATE(createddate) = CUR_DATE()
'2014-05-02' = '2014-05-02'
v.s.
... WHERE createddate = now()
'2014-05-02 22:14:00' = '2014-05-02 01:02:03'
My guess is that this isn't exactly what you want: NOW() function will return the exact timestamp for when the query is run, which means you are asking it for any records created at that exact moment in time.
You may want to try something more like:
SELECT * FROM timekeeping WHERE createddate = YOUR_DATE_CRITERIA
I store standard date ( with this format: ('Y-m-d H:i:s') ) in mysql database, now i want to select records that match this standard date with current date, in other word i want to select the rows where standard_date field demonstrate today's date.
use DATE() to strip off time in the datetime column. CURDATE() returns the current date.
SELECT *
FROM tableName
WHERE DATE(standard_date) = CURDATE()
SQLFiddle Demo (DATE() vs without DATE())
Just use:
select * from mytable where date(standard_date) = curdate();
select * from tablename where date = CURDATE()
CURDATE() returns the current date.
If you use DATE type, use CURDATE() function -
SELECT * FROM table WHERE date_field = CURDATE()
If you use DATETIME type, use CURDATE() function and DATE() function to get date part from datetime value -
SELECT * FROM table WHERE DATE(date_time_field) = CURDATE()
My table is using a datetime (YYYY-MM-DD HH:MM:SS) and i need to display today's entries.
my code is only :
SELECT *
FROM table
WHERE date = '$date'
ORDER BY score DESC
with
$date = date("Y-m-d");
well, as expected it doesnt work :| you guys have a solution here ?
Following from Pascal Martin, you could extract the date part from the date+time field:
SELECT * FROM table WHERE DATE(date) = '2009-12-19'
Source: MySQL - Date and Time Functions
Be aware however, that this query will not use an index on your date+time field, if you will be having one. (Stack Overflow: How does one create an index on the date part of DATETIME field in MySql)
Your date is "2009-12-19" (or something like that, depending on the day), which is interpreted as "2009-12-19 00:00:00".
In your database, you probably don't have any date that's exactly equal to that one, by the second : your dates are like "2009-12-19 12:15:32".
A solution is to compare like this :
select *
from table
where date >= '2009-12-19'
and date < '2009-12-20'
Which will be interpreted as :
select *
from table
where date >= '2009-12-19 00:00:00'
and date < '2009-12-20 00:00:00'
And, if you don't want to do the math to get the date of the following date, you can use the adddate function :
select *
from table
where date >= '2009-12-19'
and date < adddate('2009-12-19', interval 1 day)
So, in your case, something like this should do the trick :
select *
from table
where date >= '$date'
and date < adddate('$date', interval 1 day)
order by score desc
You probably want to format the data when you select it:
SELECT *, DATE_FORMAT(date, '%Y-%m-%d') AS dateformat FROM table
WHERE dateformat = '$date' ORDER BY score DESC
You are comparing datetime and date expression, that is why its not working. Use Date() method to return the date part from datetime and then do the comparison. WHERE DATE(date) = '$date' should do. You might have to use aliases to handle this name collision.