Function to get date from datetime in SQL Server

Hi,

Many a times need arises to get only date part from Datetime datatype/column/value. This blogs lists functions to get date from datetime in SQL Server.

-- SQL Server 2008 onwards
SELECT CONVERT(DATE,GETDATE()) AS Date_part
GO
DECLARE @Date_part Date
SET @Date_part=GETDATE()
SELECT @Date_part AS Date_part
GO

-- All SQL Server versions 
SELECT CONVERT(datetime,
			CONVERT(VARCHAR(10),GETDATE(),101)) 
	   As Date_part
GO
SELECT 
	DATENAME(YYYY,GETDATE()) + '-'
	+ DATENAME(MM,GETDATE())	 + '-'
	+ DATENAME(DAY,GETDATE()) 
	AS Date_part
GO
SELECT DATEADD(dd, 0, 
        DATEDIFF(dd, 0, GETDATE())) 
	AS Date_part
GO
SELECT 
	CONVERT(Datetime,DATEDIFF(DD, 0, GETDATE()))
	AS Date_part

The output from above queries is shown below.

   

function to get date from datetime in sql server

 

Like us on FaceBook Join the fastest growing SQL Server group on FaceBook

   

Leave a Reply

Your email address will not be published.