6

I have a mysql query that has a column CreationDate in the format of "timestamp". Example:

2013-03-27 18:32:45

I would like for the sql query to format it in this fashion (12hr format with seconds):

Friday 3/28/2013 12:52:34 PM

I would like to handle this at the sql level rather than php or .js, etc.

Query:

SELECT a.ID, a.CreationDate, a.Content, a.Type, u.Nickname
FROM Announcements a 
INNER JOIN Accounts u ON a.FromAccountID = u.AccountID
WHERE a.Status  = '1' AND u.Status = '1'
ORDER BY a.ID DESC
1

2 Answers 2

8

You will want to use DATE_FORMAT:

SELECT a.ID, 
  date_format(a.CreationDate, '%W %m/%d/%Y %l:%i %p') CreationDate, 
  a.Content, 
  a.Type, 
  u.Nickname 
FROM Announcements a 
INNER JOIN Accounts u 
  ON a.FromAccountID = u.AccountID 
WHERE a.Status = '1' 
  AND u.Status = '1' 
ORDER BY a.ID DESC

The MySQL docs will show what specifiers you will use to get the format that you want (See SQL Demo).

If you want to keep seconds, then you will use:

date_format(a.CreationDate, '%W %m/%d/%Y %r')
Sign up to request clarification or add additional context in comments.

4 Comments

Is there any way to keep the seconds in this Friday 3/28/2013 12:52:02 PM
@user1157800 See my edit, you will use '%W %m/%d/%Y %r %p' as the format
I am sorry but how can I keep 12 hour format with AM/PM respectfully?
@user1157800 Sorry I used the wrong one '%W %m/%d/%Y %r %p'
1

DATE_FORMAT(a.CreationDate,'%W %c/%e/%Y %h:%i %p')

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.