10

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!

shA.t
  • 16,580
  • 5
  • 54
  • 111
Newkidirus
  • 141
  • 1
  • 1
  • 5
  • Are you looking for a string representation in this specified format? – Martin Smith Oct 05 '11 at 16:37
  • does the requirement dictate you not being able to fix this in code? – jim Oct 05 '11 at 16:38
  • If you need this value as a TIME then I'd use the DATEDIFF answer from @Wil. If this really is about formatting strings, then I'd recommend you do that outside of SQL, such as in your client application. – MatBailie Oct 05 '11 at 16:46
  • I will resign myself to this being a limitation of SQL (a silly formatting issue, but all the same) I will go ahead and do it in php. Thank you anyway – Newkidirus Oct 05 '11 at 16:51

8 Answers8

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

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

Jamiec
  • 133,658
  • 13
  • 134
  • 193
11

You might want to check out this old thread.

If you can omit AM/PM portion and using SQL Server 2008, you should go with the approach suggested here

To get the rid from nenoseconds in time(SQL Server 2008), do as below :

SELECT CONVERT(TIME(0),GETDATE()) AS HourMinuteSecond

I hope it helps!!

Community
  • 1
  • 1
Nilesh Thakkar
  • 2,877
  • 1
  • 24
  • 43
  • If you're converting it to varchar, it will no longer be datetime. Please check this [link](http://weblogs.sqlteam.com/jeffs/archive/2007/04/13/format-date-sql-server.aspx) – Nilesh Thakkar Oct 05 '11 at 16:59
6

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.

Marcelo
  • 61
  • 1
  • 2
3

To get the format you want:

SELECT (substring(CONVERT(VARCHAR,GETDATE(),22),10,8) + ' ' + SUBSTRING(CONVERT(VARCHAR,getdate(),22), 19,2))

Why are you pulling this from sql?

k.parnell
  • 2,617
  • 2
  • 16
  • 12
  • I hope to have a clock in php pulling time that is "live" but pulled from sql. (I'm using sqlsrv) – Newkidirus Oct 05 '11 at 16:41
  • 1
    You are much likely better off, from many perspectives, doing the formatting in PHP then. Let your SQL be about data and move the DATE and/or TIME values around as real values, not strings. Keeping your data and your presentation separated is very good form. – MatBailie Oct 05 '11 at 16:47
  • @Dems, I considered this, and can and have done this in the past, but I need it to come from my computer, not the computer viewing it. – Newkidirus Oct 05 '11 at 16:49
  • @Newkidirus you mean you need the time on the SQL box not the PHP box? – k.parnell Oct 05 '11 at 16:52
  • 1
    @Newkirirus - You mis-understand me. Use GetDate() to get the date from the SQL server by all means. But then format it as a string in PHP. SQL *can* format, but it isn't designed *for* formatting, it's for *data*. – MatBailie Oct 05 '11 at 17:44
1

Let's try this

select convert(varchar, getdate(), 108) 

Just try a few moment ago

Ardi
  • 65
  • 7
0

You will be able to get the time using below query:

select left((convert(time(0), GETDATE ())),5)
Adrita Sharma
  • 21,581
  • 10
  • 69
  • 79
0

You can use the datapart to maintain time date type and you can compare it to another time.
Check below example:

declare @fromtime time = '09:30'
declare @totime time
SET @totime=CONVERT(TIME, CONCAT(DATEPART(HOUR, GETDATE()),':', DATEPART(MINUTE, GETDATE())))
if @fromtime <= @totime 
begin print 'true' end
else begin print 'no' end
סטנלי גרונן
  • 2,917
  • 23
  • 46
  • 68
0

If it's SQL Server 2005 there is no TIME datatype. The easiest way to get only the time component is to set the date to 1/1/1900.

DECLARE @time DATETIME

SET @Time = GETDATE()

SELECT DATEADD(dd,DATEDIFF(dd,@time,'1/1/1900'),@time)
Wil
  • 4,130
  • 1
  • 16
  • 15