Tuesday, 26 June 2018

How to get Time, Hour, Minute, Second and Millisecond Part from DateTime in Sql Server

selecting only hour and minute from datetime. - SQL Server

Description:

In this example we explain that how to get Time, Hour, Minute, Second and Millisecond from DateTime in SQLServer.or SQL Query to get Time, Hour, Minute, Second part from DateTime Field in SQL Server.

In our requirement we have to display only those record has time was after 5:30 so we have to first get the Hours and Minutes from the DateTime Field and then we have to check the condition that it is greater than that then it will be listed in select query.

So below the list of the SQL Query that will be helpful to you to get the all that Date and Time part from date using SQL Query.
Query:

--Time in the 24-hour format hh:mi:ss:mmm
SELECT GETDATE() 'Today', CONVERT(VARCHAR(12),GETDATE(),114) 'hh:mi:ss:mmm'
--Time in the 12-hour format hh:mi:ss:mmmAM (or PM)
SELECT  GETDATE() 'Today', RIGHT(CONVERT(VARCHAR(26), GETDATE(), 109),14) 'hh:mi:ss:mmmAM (or PM)'
--Time in the 12-hour format hh:miAM (or PM)
SELECT LTRIM(RIGHT(CONVERT(VARCHAR(20), GETDATE(), 100), 7))
--Time in the 24-hour format hh:miAM (or PM)
SELECT  GETDATE() 'Today',CONVERT(VARCHAR(5), GETDATE(), 108) + (CASE WHEN DATEPART(HOUR, GETDATE()) > 12 THEN 'PM'  ELSE 'AM' END) 'hh:miAM (or PM)'



This entry was posted in :

0 comments:

Post a Comment