Format date in SQL

Posted on 2014-08-22
Last Modified: 2014-08-22
I have a datetime field (MS SQL 2008) and I would like to format it like this: 8/22/2014 10:25

I am trying to format it like: mm/dd/yyyy hh:ss (or mm-dd-yyyy hh:ss would be fine too.)

I tried using "convert" but I am not having any luck.

Could someone assist?
Question by:earwig75
    LVL 25

    Accepted Solution

    You might need 2 converts to get both parts. Couldn't see an obvious way.

    declare @dt datetime
    select @dt = GETDATE()
    select CONVERT(nvarchar(8), @dt, 101) + ' ' + CONVERT(nvarchar(5), @dt, 108)

    Open in new window

    LVL 69

    Expert Comment

    by:Éric Moreau
    SET @myDateTime = '8/22/2014 10:25'
    -- Convert string
    SELECT @myDateTime, CONVERT(VARCHAR, @myDateTime, 101) + ' ' + CONVERT(VARCHAR, @myDateTime, 114)

    Open in new window

    LVL 69

    Expert Comment

    by:Éric Moreau
    damn, 30 seconds to late!
    LVL 25

    Expert Comment

    by:Shaun Kline
    If you want that format, you will need to use two separate CONVERT statements: (assuming your time format should have included minutes)


    Open in new window

    In this format, hours uses a 24 hour clock.
    LVL 47

    Expert Comment

    mm/dd/yyyy hh:ss

    why would you not want minutes?

    I'm intrigued

    btw:  if you have SQL 2012 or later:

    SELECT FORMAT(GETDATE(), 'MM-dd-yyyy HH:ss')

    SELECT FORMAT(GETDATE(), 'MM-dd-yyyy HH:mm:ss') --<< more conventionally with minutes
    SQL Server Date Styles (formats) using CONVERT()

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Highfive Gives IT Their Time Back

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
    JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
    Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.
    Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

    779 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    12 Experts available now in Live!

    Get 1:1 Help Now