11

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!

4
  • Are you looking for a string representation in this specified format? Commented Oct 5, 2011 at 16:37
  • does the requirement dictate you not being able to fix this in code?
    – jim
    Commented Oct 5, 2011 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
    Commented Oct 5, 2011 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
    Commented Oct 5, 2011 at 16:51

8 Answers 8

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

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

3
  • Ah damn.. so close but no cigar.
    – Jamiec
    Commented Oct 5, 2011 at 16:39
  • That's better, but I require the AM/PM.
    – Newkidirus
    Commented Oct 5, 2011 at 16:40
  • 1
    else can use 'convert(time, getdate())' return that answer. Commented Apr 8, 2014 at 13:53
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!!

1
  • If you're converting it to varchar, it will no longer be datetime. Please check this link Commented Oct 5, 2011 at 16:59
8

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.

3
  • this is a comment and not an answer
    – Léa Gris
    Commented Aug 29, 2019 at 19:28
  • 1
    If you try to run this code you will see an answer and not a comment.
    – Marcelo
    Commented Sep 4, 2019 at 1:03
  • Thanks @Marcelo, forgot about the format function.
    – user70267
    Commented Nov 9, 2021 at 16:36
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?

5
  • I hope to have a clock in php pulling time that is "live" but pulled from sql. (I'm using sqlsrv)
    – Newkidirus
    Commented Oct 5, 2011 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
    Commented Oct 5, 2011 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
    Commented Oct 5, 2011 at 16:49
  • @Newkidirus you mean you need the time on the SQL box not the PHP box?
    – k.parnell
    Commented Oct 5, 2011 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
    Commented Oct 5, 2011 at 17:44
1

Let's try this

select convert(varchar, getdate(), 108) 

Just try a few moment ago

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)
0
0

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

select left((convert(time(0), GETDATE ())),5)
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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.