Saturday 12 July 2014

How to get Date Part only from DateTime in Sql Server





Description:-

        In this example we explain that how to get only Date excluding time from DateTime in sqlserver. Or how to getting only the Date part of the DateTime stamp in sqlserver.

We all faced this problem because we are the developer and many times we pass across this scenario where we only need to get Date part only from the DateTime or TimeStamp in sqlserver.the one and only most example that we all have requirement in any application is that the B.O.D (Birth date of the user) in each and every application so that we have only display the birthdate of the employee not the time.

To Show Example of How to Upload File in MVC Application then click here upload Image and bind to Gridview in MVC


How to upload File and Fetch file from SqlServer and bind to Gridview fetch file from Sqlserver and bind to Gridview


So here is a multiple way to fetch or get only Date part of the DateTime in sqlserver are listed below



(1). SELECT CONVERT (DATE, GETDATE()) 'Return only Date Part with yyy/mm/dd format'

RESULT:
Return only Date Part with yyy/mm/dd format
--------------
2014-07-12

(2). SELECT CONVERT(VARCHAR(10), GETDATE(), 112) 'Return only Date Part with yyymmdd format'

RESULT:
Return only Date Part with yyy/mm/dd format
--------------
20140712

(3). SELECT CONVERT(VARCHAR(10), GETDATE(), 111) 'Return only Date Part with yyy/mm/dd format'

RESULT:
Return only Date Part with yyy/mm/dd format
--------------
2014/07/12

(4). SELECT CONVERT(DATETIME,CONVERT(VARCHAR(10), GETDATE(), 112)) 'Return only Date Part with yyy/mm/dd format'

RESULT:
Return only Date Part with yyy/mm/dd format
-----------------------
2014-07-12 00:00:00.000

(5). SELECT CONVERT(DATETIME,CONVERT(VARCHAR(10), GETDATE(), 111)) 'Return only Date Part with yyy/mm/dd format'


RESULT:
Return only Date Part with yyy/mm/dd format
-----------------------
2014-07-12 00:00:00.000

(6). SELECT DATEADD(dd, 0, DATEDIFF(dd, 0, GETDATE())) 'Return only Date Part with yyy/mm/dd format'

RESULT:
Return only Date Part with yyy/mm/dd format
-----------------------
2014-07-12 00:00:00.000

0 comments:

Post a Comment