Can I add time with date in mysql? - mysql

Can I add time with date in mysql ?
( e.g. date=2017-04-05 00:00:39 and time=00:10:00
result should be 2017-04-05 00:10:39 )
I have tried
SELECT dateadd(start_time,max_bidding_time) from product;

Yes, you can use ADD_TIME() function:
SELECT ADDTIME('2017-04-05 00:00:39', '00:10:00');

yes. you can change the structure of your table date field. Select type like "datetime" in database.

SELECT
ADDTIME(start_time, DATE_FORMAT(time,'%H:%i:%s')) AS updatetime
FROM product;

Related

MySQL how to get Month and Year with separation like date format

I have "datetime" field on my database,
but what I want is only take YEAR-MONTH e.g ("2012-02")
I've tried using function YEAR_MONTH but the output without "-" which is similar like "201202".
Is there any way I could make that select?
You can use the date_format function to specify how to format your columns:
SELECT DATE_FORMAT(my_datetime_field, '%Y-%m')
FROM my_table
You want to give False in second Param in Select
select("DATE_FORMAT(now(), '%Y-%m') AS dated_now", FALSE);
Try this:
SELECT DATE_FORMAT(now(),'%Y-%m');
Try this
SELECT DATE_FORMAT(now(),'%Y %m');
You can also check the below link
http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html

mysql string to date

How can I convert a string/varchar like '12-Mar-2013' to date like 2013-03-12?
I tried
SELECT STR_TO_DATE('12-Mar-2013','%Y-%m-%d');
SELECT DATE_FORMAT('12-Mar-2013','%Y-%m-%d');
but both return null.
My current database version is 5.5.7-rc.
Use STR_TO_DATE.
Try
SELECT STR_TO_DATE('12-Mar-2013','%d-%M-%Y');
-> 2013-03-12
This should do the trick
SELECT DATE_FORMAT(STR_TO_DATE('12-Mar-2013','%d-%b-%Y'), '%Y-%d-%m');

query data in mysql using a date in a datetime column

I want to query data in mysql. The problem is the column is datetime(dpost) and i will use a date in the where clause.
SELECT * FROM `uid1000_qposts` WHERE `dpost`="2011-12-06"
this query doesn't return any results. Is there any possible way I can query data using date against a datetime column?
thanks
SELECT * FROM `uid1000_qposts` WHERE date(`dpost`) = '2011-12-06'
Try this:
SELECT * FROM `uid1000_qposts` WHERE DATE_FORMAT(dpost,'%Y-%m-%d')='2011-12-06'
WHERE dpost between "2011-12-06 00:00:00" and "2011-12-06 23:59:59"
Another possibilities is using
DATE_FORMAT(dpost,'%Y-%m-%d')="2011-12-06"
but is not recommended,
as it won't make use on any index (even there is)
SELECT * FROM `uid1000_qposts` WHERE date(`dpost`) = '2011-12-06'

MySql, combining date and time column into a time stamp

I am guessing this is relatively simple to do, but I am unsure of the syntax. I have date and time columns that I want to combine to a timestamp column. how would I query this using a select?
Or you could use the built-in TIMESTAMP(date,time) function.
So then you would do something like this say from an Orders table...
SELECT OrderNumber, TIMESTAMP(date,time) as OrderTS, SalesPersonID
FROM Orders
Mysql does not seem to have a constructor for datetime such as datetime('2017-10-26', '09:28:00'). So you will have to treat the component part as string and use string concatenation function (Note mysql does not have the || operator for string concatenation). If you want the datetime type, you will have to cast it.
concat(datefield,' ',timefield) as date
select cast(concat('2017-10-26', ' ', '09:28:00') as datetime) as dt;
If it possible to use built-in function, just use it.
Any way here is an example to find records between given timestamps.
SELECT `id` FROM `ar_time` WHERE TIMESTAMP(`cdate`,`ctime`) BETWEEN fromTimeStamp AND nowTimeStamp;
For 24hr time
TIMESTAMP(Date, STR_TO_DATE(Time, '%h:%i %p'))
SELECT * FROM tablename WHERE TIMESTAMP(datecol, timecol) > '2015-01-01 12:00:00';
O.P. did say SELECT but in case anyone wants to add a timestamp column:
ALTER TABLE `t` ADD COLUMN `stamp` TIMESTAMP;
UPDATE `t` SET `stamp` = STR_TO_DATE(CONCAT(`Date`, ' ', `Time`), '%m/%d/%Y %H:%i:%s');
Adjust format strings to taste.
concat('2021-12-31', ' ', '07:00:00')
it worked in an INSERT procedure.

MySQL: Convert INT to DATETIME

I have a UNIX-type timestamp stored in an INT column in MySQL. What is the proper way to retrieve this as a MySQL DATETIME?
(I found the answer when re-scanning the MySQL Date functions, but didn't see the answer on SO. Figured it should be here.)
FROM_UNIXTIME()
select from_unixtime(column,'%Y-%m-%d') from myTable;
SELECT FROM_UNIXTIME(mycolumn)
FROM mytable
The function STR_TO_DATE(COLUMN, '%input_format') can do it, you only have to specify the input format.
Example : to convert p052011
SELECT STR_TO_DATE('p052011','p%m%Y') FROM your_table;
The result : 2011-05-00
This works for sure.
select from_unixtime(column) from table