Many
developers ask how one can get the time or specific Hour and Minute from the DateTime
field in SQL Server.
Well
answer is quite simple
-- Pass the current date and get Hour and Minute.
SELECT CONVERT(NVARCHAR(5), GETDATE(), 114) AS
"Hour:Minute"
GO
We
can also use SQL TIME type if we need to get all three components(i.e. Hour,
Minute and Second) of a date.
-- Use TIME Type to get Hour Minute and Second
SELECT CONVERT(TIME,GETDATE()) AS
"Hour:Minute:Second"
GO
|
In SQL Server 2012 there is new Format Function, with the help of this we can also
fetch out Hour and Minute from a DateTime field.
-- Use FORMAT Function to get Hour and Minute.
SELECT FORMAT(GETDATE() , 'hh:mm') AS "Hour:Minute"
GO
|
Comments
Post a Comment