开发者

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. 开发者_JS百科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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜