Search Date (now) in Specific time - mysql

I want to search a Date (now) like 2022-04-11 in specific time like 00:00:00. and in this code showing date & time 0000-00-00 00:00:00.
SELECT Process_Date, Product_Number, COUNT(*) AS TEST FROM tb_main WHERE Process_Date = DATE_FORMAT(NOW(), '00:00:00') GROUP BY Product_Number;
I have already tried this code but syntax was error.
CURDATE(), '00:00:00'

If you want to use DATE_FORMAT to get midnight of the current date, then you need a format mask for the entire timestamp:
SELECT Process_Date, Product_Number, COUNT(*) AS TEST
FROM tb_main
WHERE Process_Date = DATE_FORMAT(NOW(), '%Y-%m-%d 00:00:00')
GROUP BY Product_Number;
You could also use the TIMESTAMP function here:
SELECT Process_Date, Product_Number, COUNT(*) AS TEST
FROM tb_main
WHERE Process_Date = TIMESTAMP(CURRENT_DATE)
GROUP BY Product_Number;

Related

SQL MAX datetime - 1 day

I want to do a comparison between two dates. The highest date (currently via MAX datetime) is working, but I can't get the day after the highest date to compare the data with.
I'm using the following to get the data of the highest available date:
SELECT `datetime`, `standardSubscriptionDuration`,
SUM(`activeStandardPriceSubscriptions`) AS OneMonthActiveStandard
FROM `Subscription_totals`
WHERE `standardSubscriptionDuration` = '1 Month'
AND `datetime` = (SELECT MAX(`datetime`) AS Date FROM `Subscription_totals`)";
I already tried:
(SELECT MAX(`datetime`) -1 AS Date
But this won't give the result. How am I able to get the data of yesterday and eventually compare them?
I think that you want the following date arithmetics:
WHERE
`standardSubscriptionDuration` = '1 Month'
AND `datetime` = (
SELECT MAX(`datetime`) - interval 1 day AS Date FROM `Subscription_totals`
)

MySQL: Trying to get Current date with current hour, without minutes and seconds

I am trying this:
SELECT matchId FROM Matches where matchDate = CURRENT_DATE + HOUR(CURRENT_TIME) + ':00:00'
but cant get it to work. When I manually do this, it works:
SELECT matchId FROM Matches where matchDate = '2018-01-31 16:00:00'
I tried many variations of, containing CURDATE(), CURRENT_DATE, etc... What am I doing wrong?
You could just use NOW()
SELECT matchId FROM Matches where matchDate = DATE_FORMAT(NOW(), "%y-%m-%d %h:00:00");
You could use DATE_FORMAT, with %Y-%m-%d for date and %H for hours.
SELECT DATE_FORMAT(NOW(), "Date : %Y-%m-%d Hour : %H")
It returns
Date : 2018-01-31 Hour : 15

MySQL - BETWEEN clause not working properly with DATETIME

I have a database where it is stored the date of creation of the record. The problem is that when I try to SELECT all records within 1 year, I get no rows returned.
I created a SQLFiddle to illustrate what I have and what I'm trying to do: http://sqlfiddle.com/#!9/33a32b/8
SELECT aux.description, DATE_FORMAT(aux.date_creation, '%d/%m/%Y às %H:%i') AS `formatted_date`
FROM aux
WHERE aux.date_creation BETWEEN 'CURDATE() - INTERVAL 1 YEAR' AND 'CURDATE()'
ORDER BY `formatted_date` DESC;
This is your query:
SELECT aux.description, DATE_FORMAT(aux.date_creation, '%d/%m/%Y às %H:%i') AS `formatted_date`
FROM aux
WHERE aux.date_creation BETWEEN 'CURDATE() - INTERVAL 1 YEAR' AND 'CURDATE()'
ORDER BY `formatted_date` DESC;
The single quotes are incorrect. You have a string constant, not a date expression. Presumably, date_creation is in the past, so all you need is:
SELECT aux.description,
DATE_FORMAT(aux.date_creation, '%d/%m/%Y às %H:%i') AS formatted_date
FROM aux
WHERE aux.date_creation >= CURDATE() - INTERVAL 1 YEAR
ORDER BY aux.date_creation DESC;
Note that I also changed the ORDER BY. Normally, one wants Jan 2nd to follow Jan 1st, not Feb 1st.

Count on curdate() in mysql

I have a user_entry table which contains a date field. data type is datetime.
data base is mysql.
I want a count of current date and current month and all data of current date.
How can I get this?
I tried below query but it's not working.
select * from table
where DATE(date) = CURDATE()
SELECT date FROM `test` WHERE date = CURDATE()
or
SELECT date FROM `test` WHERE date = DATE('2016-04-04')
it's work.
if you want the number of matches:
SELECT COUNT(date) from test WHERE date = CURDATE()
What is the data type of field 'date'?
To obtain the DAY/MONTH you can use the corresponding functions
SELECT MONTH(date), DAY(date) from test
Moreover, you can use groups to create a complete report
SELECT COUNT(date), date from test GROUP BY DAY(date), MONTH(date)
i used below query and it works for me.
SELECT *
FROM `user_entry`
WHERE DATE( DATE ) = DATE( NOW( ) )

MYSQL DateTime Query to get values on the particular date

Let the table contains the fields id,amount,createdDate where createdDate is in timeStamp
**1.**em.createNamedQuery("select Sum(t.amount) from tablename t
WHERE cast(t.createdDate as date) = :createdDate")**
Let the createdDate be '2012-3-23'
Now the result would be the sum of all the amount on the particular date('2012-3-23') from time 00:00:00 to 23:59:59 .
The another Way is by
**2.em.createNamedQuery("select Sum(t.amount) from tablename t
WHERE t.createdDate between :fromDate and :todate")**
The values of fromDate is'2012-3-23 0:0:0' and todate as'2012-3-23 23:59:59'
Now the result would be the sum of all the amount on the particular date('2012-3-23') from time 00:00:00 to 23:59:59 .
The above two queries work successful.
Is there any other way to get the result without using
1.cast in the query
2.Giving the fromdate and todate with the specified time values as mentioned above.
Use DATE() function:
select DATE(createdDate), SUM(t.amount) from tableName t
where DATE(createdDate) =:createdDate
Use DATE function.
SELECT SUM(t.amount) FROM tablename t WHERE DATE(createdDate) = :createdDate