Check Only date, hours & minute in mysql query - mysql

I have date in database like 2019-05-02 12:14:20 and field name is created_date Now i want to get record with created date like 2019-05-02 12:14. Don't want to check seconds.
I've tried with separating time but it won't work could you please suggest some solution which we can achieve in single mysql query.
Thanks in advance

You can use DATE_FORMAT to check a specific part of datetime to a string:
SELECT *
FROM table_name
WHERE DATE_FORMAT(created_date, '%Y-%m-%d %H:%i') = '2019-05-02 12:14'
demo on dbfiddle.uk

Related

How to remove timestamp with the date using sql query?

I have date with timestamp eg: 2019-05-06 00:00:00
I want this date without timestamp
output: 2019-05-06
use date() function
select date("2019-05-06 00:00:00" )
You can do it in this way in MySQL
SELECT CONVERT("2019-05-06 00:00:00", date);
In MSSQL same can be done in this way
select convert(varchar, your_date, 23)
You can get help from here
You can use the MySQL function DATE_FORMAT()
DATE_FORMAT(%Y-%m-%d)
More information is here

DateTime condition [duplicate]

I get a datetime field, that's currently in the query as:
SELECT DATE_FORMAT(x.date_entered, '%Y-%m-%d') AS date FROM x ORDER BY date ASC
What I want to do is to subtract 3 hours from that date (GMT issues), but I can't do it in PHP as PHP only knows the date part, not the time.
mySQL has DATE_SUB():
SELECT DATE_SUB(column, INTERVAL 3 HOUR)....
but would it not be better to try and sort out the underlying time zone issue instead?
Assuming you have some timezone issue and know source and destination timezone, you could convert it like so
SELECT DATE_FORMAT(CONVERT_TZ(x.date_entered, 'UTC', 'Europe/Berlin'),
'%Y-%m-%d') AS date
FROM x ORDER BY date ASC;
Normal select query.
Once applied DATE_ADD() function in MySQL
select lastname,
date_add(changedat, interval -24 hour) as newdate
from employee_audit;
lastname and changedat is field name and employee_audit is table name.

select a date above 18:00 in a datetime SQL

I have a table called complaints
In complaints there is a datetime column called date.
now i need to receive back in a SQL query every inserted comlaints which came in later than 18:00 of each day.
Anyone got an idea what to do or a solution with a query?
someone said to me i could use a DATE_FORMAT.
In your SQL query use this where condition.
where time(datetimefield) > '18:00:00'
Try this it might help you.
Assuming you have your database set up for 24-hour times, you can do the following:
SELECT * FROM yourTable
WHERE HOUR(TIME(yourTable.yourDateTime)) >= 18;

Query Time range between Dates using DATETIME mysql

I have a database table that has fields as such :
TIME(Datetime) Update_ID
2013-11-25 05:00:14 XC3
2013-11-25 06:00:13 XC4
2013-11-25 06:00:19 XC5
2013-12-25 23:00:14 XC6
2013-12-25 24:00:00 XC7
So assuming i want to find a trend on the updates to know which period of the day has the a particular number of updates, what i initially think of is doing something like this :
SELECT COUNT(TIME) FROM table WHERE TIME between '06:00:00' and '12:00:00'
But this doesn't work because i think since the date is not added with the time, a default value for date is added(some date around 1970). If, i add the beginning and enddate in my query, i am afraid it won't give me the results i need.
Use
WHERE HOUR(TIME)...GROUP BY DAY(TIME)
in case you have more than 1 day
You are correct, the problem is that when you do not specify the date, a default one is added.
You can use the EXTRACT function to extract the time from a date, like this:
SELECT COUNT(*)
FROM mytable
WHERE EXTRACT(HOUR_SECOND from TIME) between 60000 and 120000
Note that the time portion in the condition is specified in a different format - i.e. as numbers, without colons and quotes.
Demo on SqlFiddle.

SQL NOW() function not working properly

I'm trying to insert the date in a a query, using the NOW() statement.
However only the Y-m-d are being inserted correctly, while the hours, minutes and seconds are all appearing zeros ( 00:00:00 )
Any reason for that?
Did you check the type of the column you are inserting into? Make sure it's datetime, not just date.
Refer to the docs for more info.
Try this :
SELECT CURRENT_TIMESTAMP
or
SELECT GETDATE()
or
Select {fn NOW()}
Note the accolades in the function.