earwig75
asked on
Format date in SQL
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?
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?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
damn, 30 seconds to late!
If you want that format, you will need to use two separate CONVERT statements: (assuming your time format should have included minutes)
In this format, hours uses a 24 hour clock.
PRINT CONVERT(VARCHAR(10), GETDATE(), 110) + ' ' + CONVERT(VARCHAR(10), GETDATE(), 108)
In this format, hours uses a 24 hour clock.
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()
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
Open in new window