Is there an easy (single query) way to do this?
I'm reading those values from a column in a table and I think that the column itself is defined as a string (can't be helped, i'm afraid).
Use UNIX_TIMESTAMP;
SELECT UNIX_TIMESTAMP('2007-11-30 10:30:19');
Update:
SELECT UNIX_TIMESTAMP(CAST(fieldName AS DATE));
SELECT UNIX_TIMESTAMP(STR_TO_DATE('08/05/10','%m/%d/%y'));
SELECT '12/31/10',
STR_TO_DATE('12/31/10', '%m/%d/%y'),
UNIX_TIMESTAMP(STR_TO_DATE('12/31/10', '%m/%d/%y'))
Both functions are covered here: http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html
Related
I'm using open source db, metabase, and I tried datediff() function and it returns in day (datediff(d1,d2)),
but I want it returns in hours:
select d1,d2,d1-d2
and it returns
'2020/01/30 T00:00:00.000Z' '2020/01/28 T19:17:39.000Z' 25,158,264
how could I do to change it in hours? Thanks in advance.
btw timestampdiff does not work in my db version, so I might need another solution
According to the manual for TIMESTAMPDIFF
SELECT d1,d2,TIMESTAMPDIFF(HOUR, d1, d2) FROM table
SELECT
TIMESTAMPDIFF(HOUR,d1,d2)
AS 'Difference in Hours';
I take it that you are using MySQL.
This is the syntax:
select TIMEDIFF('2020/01/30 T00:00:00.000Z','2020/01/28 T19:17:39.000Z')
I am trying to create a dynamic date in mysql. I have written it for mssql but having troubles with mysql.
createdate(getyear(currentdate()) ,10,01)
I have tried STR_TO_Date() but i can't make the year dynamic. Any thoughts ?
If you're looking for a MySQL-only solution to getting the current year,
SELECT EXTRACT(year FROM NOW());
should do what you want.
Or for the whole date,
SELECT CONCAT(YEAR(NOW()),'-10-01');
That worked:
SELECT STR_TO_DATE(CONCAT(10,31,EXTRACT(YEAR FROM CURDATE())), '%m%d%Y') ;
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
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.
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