I have a mysql table that has a column for "date time" in TIMESTAMP format. Is there a way to group the rows by day using that column? And in an SQL query, not grouping them in php.
Assuming your column is one of the Date and Time types:
SELECT ... GROUP BY DATE(`datetimecolumn`)
GROUP BY TO_DAYS(`datetime_column`)
If you mean you have a standard DATETIME column, you can group by using one of the functions described here.
For instance:
GROUP BY DATE(datetime_column);
If you actually have a TEXT or VARCHAR column (your answer doesn't specify), then you'll need to convert it to to a date first:
GROUP BY DATE(STR_TO_DATE(datetime_column));
Related
In mySQL I have a timestamp column named when
2015-01-07 16:43:21
My question is how using PHP/mySQL
For now I can show the results based on month number like
... where month(`when`) = '1' ...
but what if I want to show the rows of a particular date for example 2015-01-05 ?
I will pass the preferable date through a variable into the sql query.
Just wrap the date() function around your date string which is already in the YYYY-MM-DD format, and also around your timestamp field in the where clause:
select * from table where date(`when`) = date('2015-01-05');
From the documentation for date():
Extracts the date part of the date or datetime expression expr.
Here's how to convert the date via MySQL:
SELECT
DATE_FORMAT(`when`, '%Y-%m-%d') AS my_date
FROM
my_table
WHERE
MONTH(`when`) = 1;
Take a look at the MySQL documentation here for other formats: http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html#function_date-format
I have a column in my database table that holds dates in the format (mmm-yy) Ex: Aug-13. I'm trying to select all dates after Jun-12. The problem is the column is a string so when I say WHERE column > 'Jun-12' it gives me values that are alphabetically greater than Jun-12 such as May-12, which is not what i want. Any thoughts on how to go about this?
Store dates as dates, not strings. That's the recommended way of doing it and I recommend changing your code accordingly.
In this case, you could use STR_TO_DATE function to compare the dates on the fly:
SELECT * FROM table_name WHERE column > STR_TO_DATE('Jun-12','%m-%y');
Hope this helps!
In this situation you'll should be able to convert to a date and then filter as required, the below code should give you a guide,
SELECT *
FROM Table
WHERE CAST('01-' + column AS DATETIME) > '01-JUN-12'
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...
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?
In the database I have table users which have column 'birthday'
Here is the sql query that I use to fetch the results
SELECT *
FROM (`users`)
WHERE `birthday` between '12/11/1982' and '31/01/1983'
The problems is that I get records only for year 1982?
Notice that records for 1983 year exists.
What could be the problem here ?
This is just an alternative answer from Mark Byers. If the data type of your column birthday is varchar and has format of dd/MM/yyyy, please do read this; otherwise, read from Mark Byer's.
The first thing you need to do is to convert your column into DATE or DATETIME datatype by using STR_TO_DATE,
SELECT *
FROM `users`
WHERE STR_TO_DATE(`birthday`,'%d/%m/%Y')
between '1982-01-12' and '1983-12-31'
SQLFiddle Demo
SOURCES
STR_TO_DATE
DATE formats
You should put the year first in your date literals:
SELECT *
FROM `users`
WHERE `birthday` between '1982-11-12' AND '1983-01-31'
From the documentation:
Date and time values can be represented in several formats, such as quoted strings or as numbers, depending on the exact type of the value and other factors. For example, in contexts where MySQL expects a date, it interprets any of '2015-07-21', '20150721', and 20150721 as a date.
If you are using a varchar type then it still makes sense to put the date first (both in the data and in the query) because then your dates will sort correctly when using the default sorting order.