Sunday, November 13, 2016

Sql server: get Date, Time, Day, Month, Year, Hour, Minute and Second from date

In this article I am going to explain how to get Date, Time, Day, Month, Year, Hour, Minute and Second from date in Sql server.


Implementation:
Here is the query:

declare @date as datetime = getdate()
select [Today's date]= @date --2016-11-13 22:22:36.057
select [date Only] =CONVERT(varchar(10),@date,106) --13 Nov 201
select [Time only] =CONVERT(varchar(10),@date,108) --24 hour 22:22:36
Select [Time Only]=RIGHT(CONVERT(varchar(20), @date, 100), 7) --12 hour
Select [Day] =DATEPART(DAY,@date) --13
Select [Month] =DATEPART(MONTH,@date) --11
Select [year] =DATEPART(YEAR,@date) --2016
Select [Hour] =DATEPART(HOUR,@date) --22
Select [Minute] =DATEPART(MINUTE,@date) --22
Select [Second] =DATEPART(SECOND,@date) --44
Select [Milli Second] =DATEPART(MILLISECOND,@date) --943

OUTPUT:

get Date, Time, Day, Month, Year, Hour, Minute and Second from date




No comments:

Post a Comment