Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get Time from Getdate()

I would like to take the Getdate() results of,

for example

2011-10-05 11:26:55.000

into

11:26:55 AM

I have looked other places and found

Select RIGHT(CONVERT(VARCHAR, GETDATE(), 100),7)

which gives me

11:26AM

It's so close I can taste it!

like image 326
Newkidirus Avatar asked Oct 05 '11 16:10

Newkidirus


People also ask

How do I get time from Getdate?

The GETDATE() function returns the current database system date and time, in a 'YYYY-MM-DD hh:mm:ss.

How do I get the current date timestamp in SQL?

The CURRENT_TIMESTAMP function returns the current date and time, in a 'YYYY-MM-DD hh:mm:ss. mmm' format. Tip: Also look at the GETDATE() function.

How do I get time in HH MM format in SQL?

To retrieve time as HH:MM format, use the DATE_FORMAT() function. To understand the function and retrieve time, let us create a table. Insert some records in the table using insert command. Display all records from the table using select statement.


2 Answers

select convert(varchar(10), GETDATE(), 108)

returned 17:36:56 when I ran it a few moments ago.

like image 192
Jamiec Avatar answered Oct 08 '22 15:10

Jamiec


Did you try to make a cast from date to time?

select cast(getdate() as time)

Reviewing the question, I saw the 'AM/PM' at end. So, my answer for this question is:

select format(getdate(), 'hh:mm:ss tt')

Run on Microsoft SQL Server 2012 and Later.

like image 26
Marcelo Avatar answered Oct 08 '22 15:10

Marcelo