SQL NOW() function not working properly - mysql

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.

Related

Check Only date, hours & minute in mysql query

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

Select datetime according to HH:MM condition

I have a table with event and datetime. How can I simply select all events that occurred any day before 21:15 hr?
You can use the TIME() function to get just the time portion of a DateTime column and do the comparison that way:
SELECT *
FROM myTable
WHERE TIME(dateColumn) < '21:15:00';
A full list of useful MySQL date and time functions can be found at this link, which may be helpful for other comparisons in the future.

Mysql datetime between two columns

I've got two columns (both datetime) startDate and endDate in an events table.
I am retrieving the current day using the the date() function of php.
This results in for example 2013-03-12.
Now there are three possibilities of events combined with dates that occur today:
An event starts and also end on this day
An event has started earlier and ends today
An event starts today but ends in the future (>= 2013-03-13)
Now I'd like to break these all into separate queries as I'm not used to work with dates. I started with the first query, but I am already failing on that one. I've tried the following:
SELECT * FROM events WHERE (startDate= '2013-03-12' AND endDate= '2013-03-12')
aswell as:
SELECT * FROM events WHERE NOT (startDate < '2013-03-12' OR endDate > '2013-03-12')
I've tried to use DATE() aswell and to format dates like '2013-03-12%'.
I don't know why it doesn't work while i am sure there is at least 1 event that is taking place on the 12th. Any help is appreciated.
Try using the MySQL's DATE() function to trim the date columns to the just the date parts:
SELECT *
FROM events
WHERE (DATE(startDate) = '2013-03-12' AND DATE(endDate)= '2013-03-12')
You can use the DATE() function as other answers suggested, but I think this makes it hard to use an index on the columns. Instead, you can include times in your comparisons:
SELECT *
FROM events
WHERE startDate BETWEEN '2013-03-12 00:00:00' AND '2013-03-12 23:59:59'
AND endDate BETWEEN '2013-03-12 00:00:00' AND '2013-03-12 23:59:59'
The DATETIME datatype in MySQL considers the time of the day as well, so, it will not match anything.
If you don't have any use of the time part, then you can simply reduce the datatype to DATE instead of DATETIME. If not, you can use the DATE() function to get rid of the time part and only consider the date part
NOTE THIS WHEN USING between on Mysql
date_column_name between 'startDate' AND 'endDate'
NOTE : you should want to insert +1 date to endDate . Because of when you insert 2015-05-18 date to endDate.you can not get data of 2015-05-18.So you need to plus one date to endDate.

mysql timestamp compare date string

we have a store procedure, the IN parameter is DATE today. in this procedure, a aql is to compare this today value with a table which has a timestamp column.
for example:
column A
2012-12-01 00:00:00
SQL:
select * from t where A = today.
We run this procedure in phpmyadmin, it run OK. but it's not work in command line.
Why?
Guess you may need to format both dates into a common format.. To be safe you may even add Date() or str_to_Date if required...if you are not sure column A contains a proper date...
Try this please:
SELECT * FROM tablename
WHERE DATE_FORMAT(A, '%d/%m/%Y') = DATE_FORMAT(TODAY, '%d/%m/%Y');
if you meant CURDATE() by today then try this as well,
SELECT * FROM tablename
WHERE DATE_FORMAT(A, '%d/%m/%Y') = DATE_FORMAT(CURDATE(), '%d/%m/%Y');
It's possible that the dates are in different formats and that's causing them to be not equal. You can use datediff(date1, date2) = 0 to fix this.
http://www.w3schools.com/sql/func_datediff_mysql.asp

Mysql Function to find total no of seconds between two dates

Say i've a from date as 17/10/2012 and to date as 18/10/2012.How will i find total no of seconds that is available ?
Update I do not want to select a row which has exceed to date ?
Thanks in advance!
Here is the working demo.
http://sqlfiddle.com/#!2/d41d8/2869
SELECT UNIX_TIMESTAMP('2012-10-19') - UNIX_TIMESTAMP('2012-10-18') as differece_seconds;
"If UNIX_TIMESTAMP() is called with a date argument, it returns the value of the argument as seconds since '1970-01-01 00:00:00' UTC."
You can simply use it with date coulmn.
Please check : http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_unix-timestamp
Assuming your columns are proper DATE/DATETIME columns and not the date strings in your question, subtract the values of UNIX_TIMESTAMP()
SELECT UNIX_TIMESTAMP(date1_column) - UNIX_TIMESTAMP(date2_column) AS difference_seconds
If you have stored dates in the string format you posted above (which is a bad idea), you will need STR_TO_DATE() to first parse them into proper MySQL dates.
SELECT UNIX_TIMSTAMP(STR_TO_DATE('17/10/2012','%d/%m/%%Y')) - UNIX_TIMSTAMP(STR_TO_DATE('18/10/2012','%d/%m/%%Y')) AS difference_seconds
Use UNIX_TIMESTAMP() to get the seconds since epoch for a specific date. Then just subtract both values.
So assuming the values (in some DATE or DATETIME) are stored in col1 and col2 respectively, use can use something like this:
SELECT UNIX_TIMESTAMP( col1 ) - UNIX_TIMESTAMP( col2 ) AS diff FROM yourTable;
select timestampdiff(second,'2012-10-16','2012-10-18');
output will be
172800