Help with GREATEST

Hi..
I'm using the greatest function on a series of dates.
It return the greatest in a wierd non data format

greatest(pci.updateddate,a.updateddate,dt.lastupdated) as LASTUPDATED

These fields are all datetime.

I get  values like
'-20130321134745'
'-20130321123715'

How can I format them as datetime?
LVL 1
JElsterAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Daniel WilsonCommented:
What's the underlying data type for these fields?  Your result looks something like the epoch or unix time.
http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_from-unixtime

Does this solve it for you?
SELECT FROM_UNIXTIME( greatest(pci.updateddate,a.updateddate,dt.lastupdated)) as LASTUPDATED
0
JElsterAuthor Commented:
says DATEIME?

get a null back
0
Daniel WilsonCommented:
OK, let's back  up a step.  Are the values that are going into GREATEST non-null?

If you select all 3 of them for inspection ...

SELECT pci.updateddate,a.updateddate,dt.lastupdated
0
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

JElsterAuthor Commented:
I get a null using your function.

the result of

SELECT pci.updateddate,a.updateddate,dt.lastupdated

'2013-02-08 15:41:09'  , '2013-03-08 12:43:01',  '2014-03-18 19:11:11'
0
Daniel WilsonCommented:
So NULL's in the underlying data are not the issue.
http://dev.mysql.com/doc/refman/5.0/en/comparison-operators.html#function_greatest

Greatest is returning something other than datetime ... and not a valid epoch/unix time.

Let's try turning each argument into a UNIX timestamp before it is passed to GREATEST.

SELECT FROM_UNIXTIME( greatest(
   UNIX_TIMESTAMP(pci.updateddate),
   UNIX_TIMESTAMP(a.updateddate),
   UNIX_TIMESTAMP(dt.lastupdated)
)) as LASTUPDATED

Open in new window

0
JElsterAuthor Commented:
I get

'-1363888065'

????????
0
Daniel WilsonCommented:
Wow, this is not supposed to be working like that.

What version of MySql are you using?
0
GaryCommented:
MySQL stores date/time as UNIX time ergo you get 20130321134745

DATE_FORMAT(greatest(pci.updateddate,a.updateddate,dt.lastupdated),'%b %d %Y %h:%i %p') as LASTUPDATED

Reference for date parts formatting
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
MySQL Server

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.