Greetings hope to get some help from you here as i been searching high and low for this.
This query works but its not the results i wanted more correctly not the right date format i want it in.
SELECT DISTINCT colum FROM table WHERE colum IS NOT NULL
This query gives me the dates
01.04.13
02.04.13
03.04.13
30.03.13
31.03.13
I wanted it to show latest date.
This info is posted in colum that stores the info as text. It is posted in the following format
dd.mm.yy
I wanted it then to show me the results as in
03.04.13 since this is todays date. i know i can limit it to 1 but still it will show the wrong date.
Thank you again for all help so far
First, you should not be storing a date as a string, you should store a data as a DateTime datatype.
Since you are storing it as a string, you will have to convert it to a date to get the max() date value. The following uses the STR_TO_DATE() function to convert the string to a date to get the max value:
select max(str_to_date(yourdate, '%d.%m.%y')) MaxDate
from table1
See SQL Fiddle with Demo
Try
select distinct column from tab where column1 = date_format(curdate(),'%d.%m.%y') AND brukernavn is not null order by brukernavn;
1) You need to save DATES in date or datetime
2) try MAX() function in MySQL
or try to ORDER BY date DESC and LIMIT 1
3) by the way, did you try using DISTINCT with GROUP BY?
Related
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 am trying to get records from my database which are added on a specific date, for example records added on the 2013-09-05. However the records are stored in my database in datetime format so 2013-09-06 08:22:35
I have tried to use a like format thinking it might just match the first half but that makes no difference.
SELECT search_term, COUNT(search_term) as count FROM $tableName WHERE client_id = '{$client_id}' AND timedate LIKE '$dates' ORDER BY count DESC LIMIT $start, $limit
I would be grateful for suggestions or a nudge in the right direction.
Thanks
you could use DATE() to get date part from datetime, so change:
...AND timedate LIKE '$dates'...
to
...AND DATE(timedate) = '$dates'...
or you could also do:
...AND timedate LIKE '$dates%'...
You can convert date using DATE_FORMAT
Use in Query as below,
AND DATE_FORMAT(timedate, '%Y-%m-%d' ) = '$dates'
Document
All,
I have the following query:
SELECT ...
FROM....
WHERE...
ORDER BY gear_checkout_summary.check_out_date DESC";
I then have the following dates for example:
12/29/2012
11/30/2012
09/04/2012
07/21/2013
07/08/2013
06/29/2013
They are sorted this way when my results are returned. My data element is stored as VARCHAR(10) in my database since I messed up that design. Is there a way to change my query so it sorts the dates correctly?
Thanks!
Try this:
ORDER BY SUBSTR(check_out_date, 7) DESC, check_out_date DESC
This first orders by the year using the SUBSTR() function. For rows that have the same year, it then orders by the date itself. This second-level ordering doesn't require SUBSTR() because MM/DD is already ordered properly.
STR_TO_DATE will see your job
STR_TO_DATE(gear_checkout_summary.check_out_date, '%c/%e/%Y') // will give '2013-06-23' in Date format
This will convert your varchar field into date format.
This would put the date in the proper MySQL date format and then ordering would be simple.
SELECT
STR_TO_DATE(gear_checkout_summary.check_out_date,"%m/%d/%Y") AS fixed_date
...
FROM ...
WHERE ...
ORDER BY fixed_date DESC
As others have stated, you should fix the format in the database if at all anyway possible. You may need to change the insert statements, but it will be worth the extra work. Here is what you would need to do to format the dates in the database
ALTER TABLE gear_checkout_summary ADD check_out_date_temp DATE AFTER check_out_date;
UPDATE gear_checkout_summary SET check_out_date_temp = STR_TO_DATE(gear_checkout_summary.check_out_date,"%m/%d/%Y");
ALTER TABLE gear_checkout_summary DROP check_out_date;
ALTER TABLE gear_checkout_summary CHANGE check_out_date_temp check_out_date DATE;
Here is explanation of above code:
Create new column in your table.
Populate the new column with the dates from the previous column formatted correctly.
Drop the old bad formatted column.
rename new column to previous columns name.
Use str_to_date:
select *
from your_table
order by STR_TO_DATE(yourDateField,'%m/%d/%Y') desc;
This will convert the string to a valid MySQL date. Check this link: http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html#function_str-to-date
You should change your data type to DATE... it is the easiest and cleanest way to store dates.
I tried all codes and at the end I achieve my sorting on varchar date with this code...
ORDER BY SUBSTR(check_out_date, 7) DESC, ORDER BY SUBSTR(check_out_date, 1,2) DESC,ORDER BY SUBSTR(check_out_date, 4,2) DESC
First sort by year, then by month and at the end by day...
EDIT
The format in the .csv excel file for the date was 15-12-2011 and in the MySQL phpmyadmin database the column type was set to DATE so if I imported the file all data in date column changed to 0000-00-00. I did the next to fix this, went into the .csv file and selected the column for date and changed the format to 'Africa' so it would change everything from d-m-Y to Y-m-d. So in the .csv it would become 2011-12-15 instead of 15-12-2011. After importing it again the date was correctly imported without turning to 0000-00-00.
After that I Changed the ORDER BY in index.php:
$query = mysql_query("SELECT * FROM `newitems` ORDER BY startVisible DESC LIMIT $start, $per_page");
After that I changed the format date where was needed.
Available from: <?php
$timestamp = strtotime($query_row["startVisible"]);
echo date("d-m-Y", $timestamp);
?>
These scripts would first change the order by 1.year 2.month 3.day and after that change the format so it would echo 15-12-2011 instead of 2011-12-15 (how it is saved in the database)
Hope this will help others with the same problem.
Cheers.
below = question I asked
I have exported a table and modified some things that needed to be modified. Next I tried to import them into the table again but startVisible column Type was set to date. All my dates where stored as 0000-00-00. All my dates in the csv file are like 15-12-2011. I've changed the column Type to varchar and after that I could import the csv file with everything as 15-12-2011 I wanted to sort everything by date in www.domain.com/index.php. That is because the column type is not set to DATE. So I Changed the Column type from VARCHAR to DATE again and all the dates changed to 0000-00-00.
So no matter what I did everything changed to 0000-00-00. I removed everything and imported the same file and all the dates where imported correctly. So it may be a MySQL load fail or something.
I used:
$query = mysql_query("SELECT * FROM newItems ORDER BY startVisible DESC LIMIT $start, $per_page");
But all the dates are mixed now. It has to short them by d-m-Y but does not do that.
9-8-2011
9-3-2011
8-3-2011
7-6-2011
7-3-2011
7-11-2011
6-7-2011
Now everything is sorted correctly but I want to change the order it shows from 2011-12-15 to 15-12-2011. ( I MEAN ON THE WEBSITE not in MySQL ). in index.php I want to show everything as d-m-Y but I still want to sort (ORDER BY) them correctly. How to do that?
Thanks,
F4LLCON
This is because the query is now ordered by a varchar column so it is sorted alphabetically, So try to convert it to a date first:
select DATE_FORMAT(STR_TO_DATE(startVisible, '%d-%m-%Y'), '%d-%m-%Y') as startDate,...
from newItems
ORDER BY startDate DESC LIMIT $start, $per_page;
You can try MySQL function STR_TO_DATE. Here is an example query.
SELECT STR_TO_DATE(dt,'%d-%m-%Y') AS fmtDate, dt FROM test.text ORDER BY fmtDate
Prasad.
Your problem is the fact the column is no longer a date, and therefore cannot be sorted as a date. You've mentioned that it is now a VARCHAR. Ergo, it will sort alphabetically, not as a date.
The type is currently varchar, so it will use text sorting.
My advise is to add a column to your table of type date, fill it by converting the date-strings to proper dates using str-to-date() and then sort on that column. (To really do d-m-y you probably have to use
ORDER BY day(datecolumn) desc, month(datecolumn) desc, year(datecolumn) desc
But are you sure you don't just mean ORDER BY datecolumn DESC - most recent first?
You can update the columns using STR_TO_DATE
Do this.
Convert and update the columns using STR_DO_DATE1
Alter the column to date type.
If you do this in Laravel then use like this below
$covid19Result = DB::select('select DATE_FORMAT(STR_TO_DATE(your_field_name,"%d-%m-%Y"), "%Y-%m-%d") as startDate, active_people, death_people, recover_people from covid_graphs ORDER BY startDate DESC');
It works fine for me
Maybe, it will be useful to create a function (or a trigger), and use it when you need :
DROP FUNCTION IF EXISTS makedate;
CREATE FUNCTION makedate(your_field VARCHAR(10)) RETURNS DATE DETERMINISTIC
RETURN
DATE_FORMAT(STR_TO_DATE(your_field, '%d/%m/%Y' ) , '%Y-%m-%d' );
Hope it helps.
I am trying to pull records after a certain date using mysql query , the field type is date in my database and the query is
SELECT * FROM tickets WHERE created_on > 26-08-2011
But it is not working and also showing all before that date
Thanks
The date you are using is a string, so it needs to be placed inside quotes. Also, the format is the wrong way around:
SELECT * FROM tickets WHERE created_on > '2011-08-26'
For more information, see the MySQL docs. In particular, note the very first line:
The format of a DATE value is 'YYYY-MM-DD'. According to standard SQL,
no other format is permitted.
The date is defined in yyyy-mm-dd, so you should use the date as 2011-08-26.
Using a date in this format is ideal for sorting as the numbers are arranged as incremental pieces.
You have to use quotes on string values, see the post of James Allardice.
Try using quotes on the date and write dates in yyyy-mm-dd format for best results. '2011-08-26'