Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 373
Richard Avatar asked Nov 08 '10 16:11

Richard


People also ask

How do I combine a date and a TIMESTAMP?

To combine date and time column into a timestamp, you can use cast() function with concat(). select cast(concat(yourDateColumnName, ' ', yourTimeColumnName) as datetime) as anyVariableName from yourTableName; In the above concept, you will use cast() when your date and time is in string format.

How do I add a TIMESTAMP to a column in MySQL?

Here is the SQL you can use to add the column in: ALTER TABLE `table1` ADD `lastUpdated` TIMESTAMP ON UPDATE CURRENT_TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ; This adds a column called 'lastUpdated' with a default value of the current date/time.

How do you display time and date in MySQL?

MySQL retrieves and displays DATETIME values in ' YYYY-MM-DD hh:mm:ss ' format. The supported range is '1000-01-01 00:00:00' to '9999-12-31 23:59:59' . The TIMESTAMP data type is used for values that contain both date and time parts.


1 Answers

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 
like image 83
ConceptRat Avatar answered Sep 28 '22 17:09

ConceptRat