开发者

Get Time from Getdate()

I would like t开发者_开发百科o 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!


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

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


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!!


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.


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?


Let's try this

select convert(varchar, getdate(), 108) 

Just try a few moment ago


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)


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

select left((convert(time(0), GETDATE ())),5)


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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜