Friday, July 25, 2014

SQL convert a Long or integer millisecond to a date string

Assume that you have an Order table in your database. The table has an OrderDate column whose data type is integer or long representing the millisecond the order is placed :-).

On your report, you of course would like the OrderDate to be displayed as a date string instead of an integer or long. The following SQL would get it done for you.

SELECT productName, orderAmount, price,  to_char(to_date('1970-01-01 00', 'yyyy-mm-dd hh24') + orderDate/1000/60/60/24, 'MM/DD/YY') orderDate from Oder order by orderDate desc;

You may replace the 'MM/DD/YY' with any format that meets your purpose, such as 'MM/DD/YYYY HH:mm:ss'  and 'YYYY-MM-DD HH24:MI:SS am'

---------------------------------------------------------------------------------------------------------

                        
If you have ever asked yourself these questions, this is the book for you. What is the meaning of life? Why do people suffer? What is in control of my life? Why is life the way it is? How can I stop suffering and be happy? How can I have a successful life? How can I have a life I like to have? How can I be the person I like to be? How can I be wiser and smarter? How can I have good and harmonious relations with others? Why do people meditate to achieve enlightenment? What is the true meaning of spiritual practice? Why all beings are one? Read the book free here.

No comments:

Post a Comment