Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get Time from DateTime format in SQL?

People also ask

How do I get timestamp from time in SQL?

getDate() is giving current date and time. 108 is formatting/giving us the required portion i.e time in this case. varchar(8) gives us the number of characters from that portion.

How do I get only hours and minutes from datetime in SQL?

We can use DATEPART() function to get the HOUR part of the DateTime in Sql Server, here we need to specify datepart parameter of the DATEPART function as hour or hh.

How do I convert datetime to date?

To convert a datetime to a date, you can use the CONVERT() , TRY_CONVERT() , or CAST() function.


SQL Server 2008:

SELECT cast(AttDate as time) [time]
FROM yourtable

Earlier versions:

SELECT convert(char(5), AttDate, 108) [time]
FROM yourtable

Assuming Sql server

SELECT CONVERT(VARCHAR(8),GETDATE(),108)


SQL Server 2008+ has a "time" datatype

SELECT 
    ..., CAST(MyDateTimeCol AS time)
FROM
   ...

For older versions, without varchar conversions

SELECT 
    ..., DATEADD(dd, DATEDIFF(dd, MyDateTimeCol, 0), MyDateTimeCol)
FROM
   ...

The simplest way to get the time from datetime without millisecond stack is:

SELECT convert(time(0),getDate())

Try using this

  • Date to Time

    select cast(getdate() as time(0))
    
  • Time to TinyTime

    select cast(orig_time as time(0))
    

Try this, it will work:

CONVERT(VARCHAR(8),DATETIME,114)

For your reference.


Get date of server

SELECT LTRIM(RIGHT(CONVERT(VARCHAR(20), GETDATE(), 100), 7)) FROM TABLENAME WHERE ...

or

If it is stored in the table

SELECT LTRIM(RIGHT(CONVERT(VARCHAR(20), datename, 100), 7)) FROM TABLENAME WHERE ...

Result:

11:41AM