I have a SQL Query running on SQL Server 2008 R2 that returns a DATETIME column. The current formatting returns this:
2011-08-25 16:35:51.000
and what I would rather see is this:
8/25/2011 4:35 PM
Ideas of how I can modify my SELECT statement? Here a simple statement to work with…
SELECT DtCheckIn from Ticket
Thank you, Andrew
You could do the following:
Resources on date time formatting:
http://anubhavg.wordpress.com/2009/06/11/how-to-format-datetime-date-in-sql-server-2005/
http://www.mssqltips.com/sqlservertip/1145/date-and-time-conversions-using-sql-server/
EDIT – it’s a little hokey but it should give you what you are looking for.
Basically the first part
SELECT convert(varchar, getdate(), 101)gives you your date 08/25/2011.The second part
SELECT convert(varchar, getdate(), 100)gives you your date and time in the formatAug 25 2011 5:35PM. You only want the time portion which is in the formathh:mmAMorhh:mmPMthe total number of characters you want is 7. So if you change this toSELECT RIGHT(convert(varchar, getdate(), 100), 8)you will get your time5:37PM. I used 8 in myRIGHT()to get an extra space. But now you only want your time not AM/PM so add SELECT LEFT(RIGHT(convert(varchar, getdate(), 100), 8), 6) and you will get your hh:mm.Finally you want your AM/PM
SELECT RIGHT(convert(varchar, getdate(), 100), 2)this will get you the last 2 characters.Put it all together and you get:
Which gives your final product: