SQL 从 Getdate() 获取时间

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7664608/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 12:21:30  来源:igfitidea点击:

Get Time from Getdate()

sqlsql-servertsqltime

提问by Newkidirus

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

我想采取的Getdate()结果,

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!

离得这么近我都能尝到!

回答by Jamiec

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

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

17:36:56几分钟前我运行它时返回。

回答by Nilesh Thakkar

You might want to check out thisold thread.

你可能想看看这个旧线程。

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

如果您可以省略 AM/PM 部分并使用 SQL Server 2008,则应采用此处建议的方法

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

要及时摆脱纳秒(SQL Server 2008),请执行以下操作:

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

I hope it helps!!

我希望它有帮助!!

回答by k.parnell

To get the format you want:

要获得您想要的格式:

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

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

Why are you pulling this from sql?

你为什么要从 sql 中提取这个?

回答by Marcelo

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

你有没有试着不时地做演员?

select cast(getdate() as time)

select cast(getdate() as time)

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

回顾这个问题,我在结尾看到了“AM/PM”。所以,我对这个问题的回答是:

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

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

Run on Microsoft SQL Server 2012 and Later.

在 Microsoft SQL Server 2012 及更高版本上运行。

回答by A.Ghandour

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

回答by Ardi

Let's try this

让我们试试这个

select convert(varchar, getdate(), 108) 

Just try a few moment ago

刚才试了一下

回答by Techniical Ravi

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

您将能够使用以下查询获取时间:

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

回答by Wil

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

如果是 SQL Server 2005,则没有TIME数据类型。仅获取时间分量的最简单方法是将日期设置为 1/1/1900。

DECLARE @time DATETIME

SET @Time = GETDATE()

SELECT DATEADD(dd,DATEDIFF(dd,@time,'1/1/1900'),@time)