I have a field named modified which value is timestamp. i.e: 2015-11-10 07:42:56
I want to match current month only, e.g.
SELECT * FROM table WHERE modified = 2015-11
I will replace 2015-11 by current month. But how can I compare only yyyy-mm part from this timestamp value?
You can use YEAR and MONTH
SELECT * FROM table WHERE YEAR(modified) = '2015' AND MONTH(modified) = '11'
If the modified column is indexed, do something like below to get better performance:
SELECT * FROM tablename
WHERE modified >= '2015-11-01' and modified < '2015-12-01'
Note: I'm not quite sure about these date literals. (Not a MySQL user.)
Related
i have a database values values like this format
premium_paid_date
31-10-17
30-10-17
11-10-18
31-08-18
31-10-17
25-11-17
but it was stored type is var-char. my customer wants this table ORDER BY year. which mean based on last two digits and followed by month and date. how can i order this using MySQL without convert into date format
You can convert string to date in your order by statement. Like
SELECT Premium_paid_date FROM Table_Name
ORDER BY Convert( DateTime, premium_paid_date, 102) DESC
see date format
Something like:
SELECT [premium_paid_date]
FROM [dbo].[premium_paid_date]
ORDER BY SUBSTRING(Date,5,6)
then you are only ordering it by the last 2 digits of the date (the year) using the substring.
Finally i solved this issue my answer is
SELECT * FROM `tablename` order by STR_TO_DATE(premium_paid_date, '%d-%m-%Y') desc
Hello I am working with Word Press and I want to fetch records according to the the
date.MySQL database has a table wp_evr_event with end_date column which has
a number of dates in end_date column according to each record like:
1. 2017-1-4
2. 2017-1-6
3. 2017-10-10
I want to fetch those record which has End date greater than to the current
date.I used Query
SELECT * FROM `wp_evr_event` WHERE `end_date`>'2017-1-31'
But I get number of records which has the less date to the current date.
How to resolved this problem.
You need to use DATE() of mysql like below:-
SELECT * FROM `wp_evr_event` WHERE DATE(`end_date`)>'2017-1-31'
Note:- I think that end_date field is of datetime type , so that's why your code is not working
You need to use str_to_date MySQL function to convert string into date. Here's the documentation.
Your query will look like this:
SELECT * FROM wp_evr_event
WHERE str_to_date(date, '%Y-%m-%d') > '2017-1-31';
Here's the SQL Fiddle.
I have next problem:
My table date format was: LIKE 2017-01-08 18:50:25 (with time).
When i use sql query like
'SELECT date FROM table WHERE date = "2017-01-08"'
My row was empty, i need COUNT all row with same (today) date WITHOUT TIME.
Note, i will not change INSERT date time!
Use DATE() to get the date portion of the datetime field and compare it to today. Use COUNT() to get the number of records that match your query.
SELECT count(*) FROM table WHERE DATE(date) = CURDATE()
You can also replace CURDATE() with NOW(), CURRENT_DATE(), and CURRENT_DATE
You can also use it in the following way
'SELECT date FROM table WHERE date_format(date,'%Y-%m-%d') = "2017-01-08"'
the date_format is mysql function which return date according to your pattern the above pattern only return the Y-m-d from the datetime
I hope it will help you
plz change your statement equal operator to greater than
'SELECT date FROM table WHERE date > "2017-01-08"'
as by default if time portion is not present then it is putting 00:00...
I need to list and records in a table where "theDate" field is either current or future.
The "theDate" field will contain a date in this format: Year-month-day ... so something like: 2014-12-31
So I have SELECT * FROM events but I need the WHERE part so I can get only current and future dates.
How can I do this?
You can use a MySQL keyword called NOW() and a simple greater than or equal to test
WHERE theDate >= NOW()
SELECT * FROM events
WHERE date_column >= CURDATE();
date_column being the name of your column that contains the date.
CURDATE() returns the current date.
I have a table with a few records and for each of these records I've also added a UNIX_TIMESTAMP. Now, I also have a search engine for those records in which I can choose a date using a jQuery datapicker. My question is how do I make the request so that to select all timestamps from the database for a certain date.
With an index on your timestamp column you will get a faster result with:
SELECT *
FROM table_name
WHERE time_stamp_column_name >= :date_picked
AND time_stamp_column_name < :date_picked + INTERVAL 1 DAY
Where :date_picked is your bound-in picked date as a string in the format 'YYYY-MM-DD'.
You can use from_unixtime to convert it into a date
select *
from
table
where
date(from_unixtime(your_timestamp_col)) = '2014-10-01'
use unix timestamp
you can also see this