How to display recently added data by date order in MySql? - 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.

Related

Modify SQL Query Order By Date (Date filed stored as varchar(25) DD-MMM-YYYY 00:00:00)

I have the following query:
$sqlC = "SELECT id, con_number, arr_date, arr_flag, reg FROM con WHERE arr_flag=0 AND reg='".$region."' ORDER BY arr_date DESC";
Column arr_date stored as varchar(25) example 18-Jun-2007 00:00:00 (sometimes can be empty)
The current query displays all matching records, but the ORDER BY is not working, as it sorts by the first 2 digits (day). For example:
31-Aug-2021 10:06:24
30-Aug-2021 09:14:40
29-Jun-2021 06:24:47
28-May-2021 15:08:30
26-Aug-2021 08:14:54
I'm trying to change this query to have the DESC working correctly, is it possible? Any help would be greatly appreciated.
Thanks,
Joseph
Your schema is, shall we say, sub-optimal. An ideal fix would be to correct it to store the date/time vales in a DATETIME column, but mindful of your comments about other scripts, the solution for your query is to convert the text value on the fly:
SELECT id, con_number, arr_date, arr_flag, reg
FROM con
WHERE arr_flag=0
AND reg='".$region."'
ORDER BY STR_TO_DATE(arr_date, '%d-%b-%Y %H:%i:%s') DESC
Because your date is not stored as a date. You can fix this. First change the format to an accepted format:
update con
set arr_date = str_to_date(arr_date, '%d-%b-%Y %H:%i:%s');
alter table con modify column arr_date datetime;
You can also use the expression for ordering, but it is better to fix the data.

Changing a string date to date datatype

I am trying to sort a table using the column Date. It is sorted upside down i.e the date is in descending format.
However, the column Date is in text format: '31-June-2008'
How could I sort this column?
Tried this code to alter but didn't work
alter table bajaj1 modify column Date date;
Your problem is that your dates are not in a valid date format so they are being sorted as strings. So for example, 31-June-2008 is "before" 9-June-2008. To sort them correctly you need to convert them to dates, which you can do using STR_TO_DATE:
STR_TO_DATE(Date, '%e-%M-%Y')
Note I've assumed when the day of the month is less than 10, it will look like e.g. 4-March-2001. If it is like 04-March-2001 you should change %e to %d.
In a query you could use this as
SELECT *
FROM yourtable
ORDER BY STR_TO_DATE(Date, '%e-%M-%Y')
If you wanted to alter your table structure, it's probably simplest to add a generated column:
ALTER TABLE yourtable ADD sdate DATE AS (STR_TO_DATE(Date, '%e-%M-%Y'))
Then you can sort on sdate instead of Date.
Demo on dbfiddle

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).

Order date stored as VARCHAR in MySql?

The .NET code, DateTime.Now.ToString() outputs something like this:
11/28/2011 1:17:05 PM
I store it in MySQL as a VARCHAR.
When I SELECT it back to my DataTable, I would like it to be ordered. Of course since it's a VARCHAR, a simple ORDER BY will sort it alphabetically and not chronologically.
Is there a way to ORDER BY this chronologically, using SQL?
You will want the STR_TO_DATE function
SELECT columns
FROM table
ORDER BY STR_TO_DATE(varCharDateColumn, '%m/%d/%Y %h:%i:%s')