Get Time part from Datetime in MS SQL server

 TIME PART FROM DATETIME: This article shows how to get only time part from DateTime in MS SQL Server. While development sometimes we need to take solely Time part from the whole Datetime. That is if the given date value is  2017-01-01 09:32:15.000  and here we want to fetch only time part for this given date that is `09:32` time as result. So mostly we developer do this part with server-side coding by using split function etc. But here we see how to get time part from datetime in MS SQL SERVER using simple query.

But here we see how to get time part from datetime in MS SQL SERVER using simple query.

SQL QUERY:

//*
select GETDATE() as default_date,
       cast(DATEPART(HOUR,GETDATE()) as varchar(2))+':'+ cast(DATEPART(MINUTE,GETDATE()) as varchar(4)) as time_part
//*

Output :

Get Time part from Datetime in Ms Sqlserver Get Time part from Datetime in MS SQL server

Here we used MS SQL  datepart() function to get hours and minutes, and then cast as varchar so we concatenate it, as a result finally we get only time part from given date.

DATEPART() function is used to return a single part of a date/time, such as year, month, day, hour, minute, etc.


In MS Sqlserver 2008 we get the time part from DateTime by using query as follow

//*
select GETDATE() default_date,
       cast(GETDATE() as time) time_part
//*

Output:

time part from datetime in ms sqlserver 2008 Time part from DateTime in ms SQL server 2008

Hope you like this tutorial. If you have any recommendations, please let us know what you think in the comment section below! See you again next time!

Sharing is Sexy:    
Satinder Singh

Hi all, am Founder and Author of Codepedia.info, also atypical polyglot programmer from Bombay, India. I have a passion to create, solve, and deploy software applications.
       ForEach ( minute in MyLife ) { myExperience++ ; }



Post Comment


Your email address will not be published. Required fields are marked *