This could be done in multiple ways and differently depending on the SQL Server edition you are using
For example
in SQL Server 2005/2008
SELECT GETDATE() as "Date"
GO
SELECT CONVERT(VARCHAR(5), GETDATE(), 114) as "Hour:Minute";
GO
returns
The same in SQL Server 2012 can be done using the new FORMAT functions
SELECT FORMAT(GETDATE() , 'hh:mm') as “Hour:Minute”;
Similarly as explained by Madhivanan in this post, there are several ways to format dates in various formats using the new SQL Server 2012 FORMAT functions
**Query 1**: SELECT FORMAT(GETDATE(),'dd-MM-yyyy') AS [dd_mm_yyyy] **[Results]**: | DD_MM_YYYY | |------------| | 19-09-2013 | **Query 2**: SELECT FORMAT(GETDATE(),'MM/dd/yyyy') AS [mm/dd/yyyy] **[Results]**: | MM/DD/YYYY | |------------| | 09/19/2013 | **Query 3**: SELECT FORMAT(GETDATE(),'MMM-yyyy') AS [MMM-yyyy] **[Results]**: | MMM-YYYY | |----------| | Sep-2013 | **Query 4**: SELECT FORMAT(GETDATE(),'MMM dd,yyyy') AS [MMM dd,yyyy] **[Results]**: | MMM DD,YYYY | |-------------| | Sep 19,2013 | **Query 5**: SELECT FORMAT(GETDATE(),'HH:mm:ss') AS [HH:mm:ss] **[Results]**: | HH:MM:SS | |----------| | 10:02:25 | **Query 6**: SELECT FORMAT(GETDATE(),'dddd, dd MMMM yyyy') AS [long format] **[Results]**: | LONG FORMAT | |-----------------------------| | Thursday, 19 September 2013 |
1 comment:
Excellent Post Super Tim
Post a Comment