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
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 get a datetime field, that's currently in the query as:
SELECT DATE_FORMAT(x.date_entered, '%Y-%m-%d') AS date FROM x ORDER BY date ASC
What I want to do is to subtract 3 hours from that date (GMT issues), but I can't do it in PHP as PHP only knows the date part, not the time.
mySQL has DATE_SUB():
SELECT DATE_SUB(column, INTERVAL 3 HOUR)....
but would it not be better to try and sort out the underlying time zone issue instead?
Assuming you have some timezone issue and know source and destination timezone, you could convert it like so
SELECT DATE_FORMAT(CONVERT_TZ(x.date_entered, 'UTC', 'Europe/Berlin'),
'%Y-%m-%d') AS date
FROM x ORDER BY date ASC;
Normal select query.
Once applied DATE_ADD() function in MySQL
select lastname,
date_add(changedat, interval -24 hour) as newdate
from employee_audit;
lastname and changedat is field name and employee_audit is table name.
How can I get the result of the current year using SQL?
I have a table that has a column date with the format yyyy-mm-dd.
Now, I want to do select query that only returns the current year result.
The pseudo code should be like:
select * from table where date is (current year dates)
The result should be as following:
id date
2 2015-01-01
3 2015-02-01
9 2015-01-01
6 2015-02-01
How can I do this?
Use YEAR() to get only the year of the dates you want to work with:
select * from table where YEAR(date) = YEAR(CURDATE())
Using WHERE YEAR(date) = YEAR(CURDATE()) is correct but it cannot use an index on column date if exists; if it doesn't exist it should.
A better solution is:
SELECT *
FROM tbl
WHERE `date` BETWEEN '2015-01-01' AND '2015-12-31'
The dates (first and last day of the year) need to be generated from the client code.
When I tried these answers on SQL server, I got an error saying curdate() was not a recognized function.
If you get the same error, using getdate() instead of curdate() should work!
--========= Get Current Year ===========
Select DATEPART(yyyy, GETDATE())
SELECT id, date FROM your_table WHERE YEAR( date ) = YEAR( CURDATE() )
SELECT
date
FROM
TABLE
WHERE
YEAR (date) = YEAR (CURDATE());
If the date field contains a time component, you want to include December 31 so you have to go to January 1 of the next year. You also don't have to use code to insert dates into the SQL. You can use the following
SELECT * FROM table
WHERE date BETWEEN MAKEDATE(YEAR(CURDATE()), 1) AND MAKEDATE(YEAR(CURDATE())+1, 1)
This will give you January 1st of the current year through January 1st at midnight of the following year.
As #Clockwork-Muse pointed out, if the date field does not contain a time component, you would want to exclude January 1 of the following year by using
WHERE date >= MAKEDATE(YEAR(CURDATE()), 1) AND date < MAKEDATE(YEAR(CURDATE())+1, 1)
You can do this using SQL DATE_FORMATE(). like below:
SELECT
date
FROM
TABLE
WHERE
DATE_FORMAT(date, '%Y') = YEAR (CURDATE());
SELECT [ID]
,[datefield]
FROM [targettable]
WHERE DATEPART(YYYY, [datefield]) = (SELECT TOP 1(MAX(DATEPART(YYYY, [datefield])))
FROM [targettable]
)
/*
This will find the newest records in the table regardless of how recent the last time data was entered.
To grab the oldest records from the table do this
SELECT [ID]
,[datefield]
FROM [targettable]
WHERE DATEPART(YYYY, [datefield]) = (SELECT TOP 1(MIN(DATEPART(YYYY, [datefield])))
FROM [targettable]
)
*/
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())
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.