I am trying to display the number of records for the current month for a specific user. Unfortunately, it does not work, because At one time, the date format in the Database was not correctly indicated.
The text column, the date is written in plain text format.
How to be? I tried a lot, nothing comes out.
I try STR_TO_DATE, convert and other - the output is either null or mysql error.
I also tried to manually enter a request into phpmyadmin. Unsuccessfully.
Mysql version: 5.6.39-83.1 (Linux)
$modercount = mysqli_query($link, 'SELECT moder, COUNT(*) FROM my_logs WHERE server="'.$profileid.'" AND MONTH(time) = MONTH(NOW()) AND YEAR(time) = YEAR(NOW()) GROUP BY moder ORDER BY COUNT(*) DESC') or die(mysqli_error($link));
Date format in my sql column: 10.04.2018 12:52:18
First of all, get your textually formatted dates translated correctly to DATETIMEs. STR_TO_DATE() uses the same format strings as its reverse, DATE_FORMAT().
SELECT STR_TO_DATE(`time`, '%Y.%m.%d %H:%i:%s')
Second, select the range of datestamps you need like this, using LAST_DAY() to handle month arithmetic.
WHERE STR_TO_DATE(`time`, '%Y.%m.%d %H:%i:%s')
>= LAST_DAY(CURDATE) + INTERVAL 1 DAY - INTERVAL 1 MONTH
AND STR_TO_DATE(`time`, '%Y.%m.%d %H:%i:%s')
< LAST_DAY(CURDATE) + INTERVAL 1 DAY
When you get a chance, add a new column to your table with the datestamps in DATETIME or TIMESTAMP format. That way your date-range selections can exploit indexes. How to do that is beyond the scope of your question today.
you should use str_to_date this way
select str_to_date('10.04.2018 12:52:18', '%d.%m.%Y %T')
This works for me.
$modercount = mysqli_query($link, 'SELECT moder, COUNT(*) FROM mylogs WHERE server="'.$profileid.'" AND MONTH(str_to_date(`time`, "%d.%m.%Y %T")) = MONTH(NOW()) AND YEAR(str_to_date(`time`, "%d.%m.%Y %T")) = YEAR(NOW()) GROUP BY moder ORDER BY COUNT(*) DESC') or die(mysqli_error($link));
Big thanks to #scaisedge and #o-jones
Related
I'm a bit confused on how to filter records by date formats.
I have date column of date(yyyy-mm-dd) data type in date table.
Ex:
date
-----
2017-01-29
2017-01-30
I'm want to change the format (dd-mm-yyyy). I'm using this code
SELECT
DATE_FORMAT(`date`, '%d-%m-%Y') as date
FROM
dim_date;
date
-----
29-01-2017
30-01-2017
I want to filter the records with the(dd-mm-yyyy) format. So I tried this code.
SELECT
DATE_FORMAT(`date`, '%d-%m-%Y') date
FROM
dim_date
WHERE DATE_FORMAT(`date`, '%d-%m-%Y') BETWEEN '20-04-2015' AND '06-09-2017';
Results
Nothing
But if I try to filter with the original format (yyyy-mm-dd) It Works.
SELECT DATE_FORMAT(`date`, '%d-%m-%Y') date
FROM dim_date
WHERE DATE_FORMAT(`date`, '%Y-%m-%d') BETWEEN '2015-04-20' AND '2017-01-07';
Why is this weird behavior in Mysql? Am I missing something here?
I also tried with this format DATE_FORMAT(date, '%d-%c-%Y') , DATE_FORMAT(date, '%d-%l-%Y') & DATE_FORMAT(date, '%e-%l-%Y')
No happy face, Please let me known.
Thanks
Max
with your query you convert the date in string and then you are comparing values using between in wrong order ('20' > '06' ) so don't work.
Between require first the min value and second the max value.
If you are working with date you sould convert the string date
so the where between work correcly and avoid the string behavior is filter
SELECT date
FROM dim_date
WHERE date BETWEEN str_to_dat('20-04-2015', '%d-%m-%Y')
AND str_to_date('06-09-2017', '%d-%,-%Y');
I Have one column in the table called "date". In this column there are some dates stored from different months. I want to display only those dates whose month is '10'. How can we write query to print the desired output using substring() function of mysql ? Or else any other solution for this ?
O/p : first 8 records from this column.
MySQL has a lot of options for working with dates. In this case, combining the MONTH() function with STR_TO_DATE() would be easiest; MONTH() takes a date as input and returns the month number, starting at 1 for January, while STR_TO_DATE() will format your (non-standard) date string into a date MySQL understands.
Your query would then become:
SELECT * FROM your_table WHERE MONTH(STR_TO_DATE(`date`, '%d-%m-%Y')) = 10;
select * from your_table where month(date) = 10;
Use DATE_FORMAT then DATE_FORMAT(date, '%m') to be able to get the value of month alone
SELECT * FROM tbl WHERE DATE_FORMAT(date, '%m') = '10'
SELECT `date` FROM table_name
WHERE MONTH(`date`) = '10'
ORDER BY `date` -- without this, the a random 8 would be delivered
LIMIT 8
If date column is datetime or Time stamp then
SELECT `date` FROM `tablename` WHERE month(`date`) =10
The dates in my database are stored as varchars instead of date formats due to the way it was first built.
The dates look like this:
e.g. 1/3/2015 and
10/3/2015
I'm trying:
"SELECT COUNT(*) n FROM tracker WHERE TIMESTAMP(STR_TO_DATE(date, '%d/%m/%Y'))<=NOW()"
However, that's not working. It is returning the count of all records, regardless of the date.
How can I count only the records where the date is today or in the past?
You do not need TIMESTAMP():
SELECT COUNT(*) as n
FROM tracker
WHERE STR_TO_DATE(date, '%d/%m/%Y') <= NOW()
You should pay attention to the functions STR_TO_DATE and NOW(), the first return a date, the second is a timestamp.
When you convert STR_TO_DATE(date, '%d/%m/%Y') you will get a date with hours, minutes and seconds as 00:00:00
Using CURRENT_DATE perhaps will match more closely the original requirements
SELECT COUNT(*) as n
FROM tracker
WHERE STR_TO_DATE(date, '%d/%m/%Y') <= CURRENT_DATE
Also I suggest you to rename the column 'date'
I am writing a Python code where I need to use MySQLdb to retrieve data from a MySQL database. A part of the original database looks like this:
I used this command
SELECT TimeStamp,Pac
FROM SolarData
WHERE DATE(`TimeStamp`) = CURDATE()
GROUP BY HOUR(TimeStamp);
to group the data by hour, but the result is not what i expected:
The Pac number shown for every hour is the same number as the first record of each hour. It's not an accumulated number for the whole hour. What I need is an accumulated number of the whole hour.
That's because MySQL is like your alcoholic uncle when you don't use GROUP BY by the ANSI standard. You probably want:
SELECT HOUR(TimeStamp) AS Hour,
SUM(Pac) AS Pac
FROM SolarData
WHERE `TimeStamp` >= CURDATE()
AND `TimeStamp` < CURDATE() + INTERVAL 1 DAY
GROUP BY HOUR(TimeStamp);
It would be helpful to see the desired result you're looking for. Until then, the above query is just a guess based on group the data by hour. For future reference, use SQL Fiddle to post your table structure/data.
"How do I decorate the code so that the hour format can have the date on it as well like this 2014-01-14 07:00"
All of Hour belong to today (CURDATE()), and second part is always ':00', so following query might help you. Could try this?
SELECT CONCAT(CURDATE(), ' ', Hour, ':00'), Pac
FROM (
SELECT HOUR(TimeStamp) AS Hour,
SUM(Pac) AS Pac
FROM SolarData
WHERE DATE(`TimeStamp`) = CURDATE()
GROUP BY HOUR(TimeStamp)
) x;
I have a table, which has several thousand rows in it, each of which has a time stamp in this format:
** YYYY-MM-DD HH-MM-SS** so 2014-01-10 04:20:26
Is it possible to use a simple method to get MySQL Select, presumably using WHERE, to get all articles, saved in January say. Alternatively I guess the only other option is to loop through each row, split the string and add it to another string if it is. Not ideal to say the least.
Note: While I can change this table the data is being fed from a third party so it isn't possible to save the month/year in its own cell.
SELECT * FROM myTable WHERE date >= '2014-01-01' AND date < '2014-02-01'
Use STR_TO_DATE to convert the string to a DATE, then use the MONTH function to extract the month and check against that.
For example:
select * from tbl where month(str_to_date(date, '%Y-%m-%d %h:%i:%s')) = 1
And for month and year:
select * from tbl
where month(str_to_date(date, '%Y-%m-%d %h:%i:%s')) = 1
and year(str_to_date(date, '%Y-%m-%d %h:%i:%s')) = 2014