Tuesday, June 22, 2010

T-SQL Format DateTime

Convert SQL DATE/Times to commonly used formats

-- Date Format MM/DD/YYYY
SELECT CONVERT(varchar(10),getdate(),101)

-- Date Fromat MM/DD/YYYY HH:MM

SELECT CONVERT(char(10), getdate(), 101)
+ ' ' + CONVERT (char(5), getdate(), 108);


SELECT CONVERT(VARCHAR, GetDate(), 101) + ' ' +
CONVERT(VARCHAR, DATEPART(hh, GetDate())) + ':' +
RIGHT('0' + CONVERT(VARCHAR, DATEPART(mi, GetDate())), 2) AS Date


SELECT CONVERT(CHAR(11),GETDATE(),101)
+ CONVERT(CHAR( 5),GETDATE(),114)


--- Date Format MM/DD/YYYY HH:MMAM/PM

SELECT CONVERT(VARCHAR(10), GETDATE(), 101) + ' ' + RIGHT(CONVERT(VARCHAR, GETDATE(), 100), 7)