2

How do I convert timestamps in a MySQL View?

I have a MySQL View for data exchange between 2 different software tools. The original table is a unix timestamp as int(11). The other software expects a DATETIME.

I have a VIEW like this:

CREATE VIEW `myView` AS 
select 
date_format(from_unixtime(`myUnixTimestamp`),_utf8'%d/%m/%Y %H:%i:%s') 
AS `Date-Entered`
from ...

This VIEW statement creates a varchar(24) - which is better than nothing as it can be converted - but the conversion makes some problems.

Is there any way to create a DATETIME field instead of a VARCHAR with the view?

3 Answers 3

3

You can cast the value to datetime:

-- print a UNIX timestamp (as returned by NOW()) as DATETIME
SELECT CAST( DATE_FORMAT( NOW( ) , '%Y-%m-%d' ) AS DATETIME );
Sign up to request clarification or add additional context in comments.

Comments

2

Thanks for the answers, I tried some combinations, and this one worked (so easy, its a shame...):

CREATE VIEW `myView` AS 
select 
from_unixtime(`myUnixTimestamp`)
AS `Date-Entered`
from ...

View Structure shows: Date-Entered, type: datetime

Comments

-1

try

CREATE VIEW `myView` AS 
select 
FROM_UNIXTIME([unix timestamp],'%d/%m/%Y %H:%i:%s') 
AS `Date-Entered`
from ...

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.