Wednesday, August 5, 2009

SQL SERVER – Get Time in Hour:Minute Format from a Datetime – Get Date Part Only from Datetime

I have seen scores of expert developers getting perplexed with SQL Server in finding time only from datetime datatype. Let us have a quick glance look at the solution.

SQL Server 2000/2005

   1:  SELECT
   2:  CONVERT(VARCHAR(8),GETDATE(),108) AS HourMinuteSecond,
   3:  CONVERT(VARCHAR(8),GETDATE(),101) AS DateOnly
   4:  GO

SQL Server 2008

   1:  SELECT
   2:  CONVERT(TIME,GETDATE()) AS HourMinuteSecond,
   3:  CONVERT(DATE,GETDATE(),101) AS DateOnly
   4:  GO

I hope the above solution is clear to you all.

Reference : Pinal Dave (