mysql match a year withing datetime field - mysql

I am wondering if this is possible, in my database table, I have a datetime field, I wanting to query that table and pull in all the rows that have a posted date of 2011, however my date posted column is dateTime, so the form looks like this,
2011-06-12 00:00:00
How can I query the table to return all 2011 rows?

SELECT * FROM yourtable WHERE YEAR(datetime_column) = '2011'
Read more about YEAR()

It should work with datetime type too.
SELECT * FROM tblName WHERE YEAR(column) = '2011'

Try with:
SELECT *
FROM your_table
WHERE EXTRACT(YEAR FROM datetime_column) = '2011'

You can even sub-string it if you want...
SELECT * FROM tblName WHERE substr(column,1,4) = '2011'
I would however recommend creating a "date dimension" table, and then splitting your dateTime field into separate date and time fields, then joining to the "date dimension" table via the date field, which allows for much better functionality. See the answer I put on the following post for more information about using a "date dimension" table.
Select all months within given date span, including the ones with 0 values
You can join your table to it as follows;
SELECT tblName.*
FROM tblName
INNER JOIN dimDates AS dd
ON tblName.dateField = dd.dd_date
AND dd.dd_date = "2011"

Related

How to display recently added data by date order in MySql?

I wish to sort my table with date order so that recently added data will be on the top of the table.
I have used query for sorting as:
select date from register_table order by date desc.
Currently table display data as:
date
02.04.2019
05.04.2019
09.04.2019
10.04.2019
06.02.2019
23.01.2019
11.01.2019
I expect my table to display as:
date
10.04.2019
09.04.2019
05.04.2019
02.04.2019
06.02.2019
23.01.2019
11.01.2019
How to display data in date order?
Your fundamental problem is not storing the date as a date. You should fix that.
For the query to work, use:
order by str_to_date(date, '%m.%d.%Y')
To fix the data, you can do:
update register_table
set date = str_to_date(date, '%m.%d.%Y');
alter table register_table
modify date date;
You can see how this works here.
I don't know why your date is stored like that but here, give this a try:
SELECT date FROM date ORDER BY STR_TO_DATE(REPLACE(date,'.','-'),'%d-%m-%Y') DESC;
If you want to see what exactly happen, run this query:
SELECT date,STR_TO_DATE(REPLACE(date,'.','-'),'%d-%m-%Y') FROM date;
In case you still don't quite understand, refer to MySQL STR_TO_DATE function and MySQL REPLACE function.

Mysql selecting unique values in date type varchar

I have a column where the dates are type varchar. For example:
15-10-2018
16-10-2018
19-10-2018
23-10-2018
29-10-2018
8-11-2018
9-11-2018
10-11-2018
12-11-2018
when I consult with the following query
SELECT DISTINCT date FROM `test` WHERE date BETWEEN '15-10-2018' and '9-11-2018'.
I have the right result.
15-10-2018
16-10-2018
19-10-2018
23-10-2018
29-10-2018
8-11-2018
9-11-2018
but if the query is:
SELECT DISTINCT date FROM `test` WHERE date BETWEEN '15-10-2018' and '10-11-2018'.
or
SELECT DISTINCT date FROM `test` WHERE date BETWEEN '15-10-2018' and '12-11-2018'.
The answer I get is empty.
I think it's only validating the days in the sql.
I need to get the right dates.
I think the problem is the fact that the column is varchar, so it's comparing characters instead of a range of dates. I will recommend convert the column to date type and try again.
Alternative if you cannot change the type of the column you could cast it to date format like this:
SELECT DISTINCT `date` FROM `test` WHERE STR_TO_DATE(`date`,'%d-%m-%Y') BETWEEN '2018-10-15' AND '2018-11-10';
I tested with your data and it works. Of course this could put some extra effort on the database and will not use indexes.
You need to set the datatype to date and update your dates to be using date for a more reliable result. Once done you should be using the database format for the dates in your WHERE clause.
Try
SELECT DISTINCT date FROMtestWHERE date BETWEEN '2018-10-15' and '2018-11-10'

sql error using between operator with date between two dates

i have an events table having start date and end date I am trying retrieve all the records by giving a date that is between start and end dates.
eg :
SELECT *
FROM `events`
WHERE '2017-01-29' BETWEEN start_date='2017-01-28'
AND end_date='2017-01-31'
but response is syntax error can any one help me to finish the query
Just list the columns.
WHERE '2017-01-29' BETWEEN start_date AND end_date
The values come from the table, you don't put them into the query.
According to mysql documentation (https://dev.mysql.com/doc/refman/5.7/en/comparison-operators.html#operator_between) the syntax for BETWEN is
expr BETWEEN min AND max
it is not
expr BETWEEN blabla=min AND stuff=max
Also, it is rather pointless to be using constants in all three expressions, because in this case the result will be known in advance (either always TRUE or always FALSE) without having to consult the values in your table.
It is kind of hard to give you an example without knowing the structure of your table, but what you probably want is something like
WHERE '2017-01-29' BETWEEN start_date
AND end_date
(assuming start_date and end_date are columns in your table)
or something like
WHERE some_column BETWEEN '2017-01-28'
AND '2017-01-31'
(assuming some_column is a column in your table.)
I believe you're trying to find all the rows where a date is 2017-01-29, and so, your query could be:
SELECT *
FROM `events`
WHERE
date = '2017-01-29';
If, however, you want all rows with date between 2017-01-28 and 2017-01-31, then you could do:
SELECT *
FROM `events`
WHERE
date BETWEEN '2017-01-28' AND '2017-01-31';
Instead of putting 2017-01-29 before WHERE, put the name of the field you want to filter by date, such as EventDate (or whatever your field is named).

Trying to get data of a column from MySQL in date format

I have a MySql table with name customer and in this, i have a column with name callback_date. It is a varchar field. And I want to get its data as DATE.
Here is how I make my query.
SELECT callback_date AS calld
FROM customer
WHERE callback_date<>''
AND STR_TO_DATE('calld', '%d/%m/%Y')
Why my data is not returning in date format of this particular column? Any specific query I need to run?
don't know what exactly you want but maybe this is what I understand, query should be:
SELECT temp.*
FROM
(SELECT callback_date AS calld,
DATE_FORMAT(callback_date, '%d/%m/%Y') AS dt
FROM customer
WHERE callback_date <> '') AS temp
WHERE temp.dt = '18/03/2016'

Querying Where date_created without time is same as last_updated without time

In MySQL, I need to write a query (if possible) that finds all rows of a table where the date_created is the same as last_updated. The rub is that I need to ignore the time. Basically, I'm looking for user rows that were created and activated the same day (we don't store an activation date). So presumably the dates would be the same but the times may be different.
You could use the DATE() function, which returns only the date portion of a datetime value. This allows you to compare just the date portion of the values:
SELECT * FROM table_name
WHERE DATE(date_created) = DATE(last_updated)
The timezone may be relevant here. So you may want to cast the datetime values to the user's timezone prior to using the DATE() function, using CONVERT_TZ().
Try this:
SELECT *
FROM table_name
WHERE DATE_FORMAT(date_created, '%Y-%m-%d') = DATE_FORMAT(last_updated, '%Y-%m-%d')
not pretty but works:
SELECT *
FROM table_name
WHERE day(date_created) = day(last_updated) and
month(date_created) = month(last_updated) and
year(date_created) = year(last_updated)