Link to home
Start Free TrialLog in
Avatar of earwig75
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?
ASKER CERTIFIED SOLUTION
Avatar of Lee
Lee
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
DECLARE @myDateTime DATETIME
SET @myDateTime = '8/22/2014 10:25'

--
-- Convert string
--
SELECT @myDateTime, CONVERT(VARCHAR, @myDateTime, 101) + ' ' + CONVERT(VARCHAR, @myDateTime, 114)

Open in new window

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)

PRINT CONVERT(VARCHAR(10), GETDATE(), 110) + ' ' + CONVERT(VARCHAR(10), GETDATE(), 108)

Open in new window


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()