SQL 从日期获取小时和分钟 (HH:MM)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14374822/
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 13:11:43  来源:igfitidea点击:

Get Hours and Minutes (HH:MM) from date

sqlsql-serversql-server-2008

提问by Rajbir Singh

I want to get only hh:mmfrom date.

我只想hh:mm从日期开始。

How I can get this?

我怎么能得到这个?

I have tried this :

我试过这个:

CONVERT(VARCHAR(8), getdate(), 108)

回答by gbn

Just use the first 5 characters...?

只使用前 5 个字符...?

 SELECT CONVERT(VARCHAR(5),getdate(),108) 

回答by Abdul Hannan Ijaz

You can easily use Format() function instead of all the casting for sql 2012 and above only

您可以轻松地使用 Format() 函数,而不是仅用于 sql 2012 及更高版本的所有转换

SELECT FORMAT(GETDATE(),'hh:mm')

This is by far the best way to do the required conversion.

这是迄今为止进行所需转换的最佳方法。

回答by Ali Issa

Another method using DATEPARTbuilt-in function:

使用DATEPART内置函数的另一种方法:

SELECT cast(DATEPART(hour, GETDATE()) as varchar) + ':' + cast(DATEPART(minute, GETDATE()) as varchar)

回答by Rejwanul Reja

If you want to display 24 hours format use:

如果要显示 24 小时格式,请使用:

SELECT FORMAT(GETDATE(),'HH:mm')

and to display 12 hours format use:

并显示 12 小时格式使用:

SELECT FORMAT(GETDATE(),'hh:mm')

回答by WillD

The following works on 2008R2+ to produce 'HH:MM':

以下工作在 2008R2+ 上生成“HH:MM”:

select
case
when len(replace(replace(replace(right(cast(getdate() as varchar),7),'AM',''),'PM',''),' ','')) = 4
then '0'+ replace(replace(replace(right(cast(getdate() as varchar),7),'AM',''),'PM',''),' ','')
else replace(replace(replace(right(cast(getdate() as varchar),7),'AM',''),'PM',''),' ','') end as [Time]

回答by Chloe Tempo

Here is syntax for showing hours and minutes for a field coming out of a SELECT statement. In this example, the SQL field is named "UpdatedOnAt" and is a DateTime. Tested with MS SQL 2014.

这是用于显示来自 SELECT 语句的字段的小时和分钟的语法。在此示例中,SQL 字段名为“UpdatedOnAt”并且是一个 DateTime。使用 MS SQL 2014 进行测试。

SELECT Format(UpdatedOnAt ,'hh:mm') as UpdatedOnAt from MyTable

I like the format that shows the day of the week as a 3-letter abbreviation, and includes the seconds:

我喜欢将星期几显示为 3 个字母缩写的格式,并包括秒:

SELECT Format(UpdatedOnAt ,'ddd hh:mm:ss') as UpdatedOnAt from MyTable

The "as UpdatedOnAt" suffix is optional. It gives you a column heading equal tot he field you were selecting to begin with.

“as UpdatedOnAt”后缀是可选的。它为您提供了一个与您选择的字段相同的列标题。

回答by Mohamad Reza Shahrestani

Following code shows current hour and minutes in 'Hour:Minutes' column for us.

以下代码为我们在“小时:分钟”列中显示当前小时和分钟。

SELECT  CONVERT(VARCHAR(5), GETDATE(), 108) + 
(CASE WHEN DATEPART(HOUR, GETDATE()) > 12 THEN ' PM'
    ELSE ' AM'
END) 'Hour:Minutes'

or

或者

SELECT  Format(GETDATE(), 'hh:mm') + 
(CASE WHEN DATEPART(HOUR, GETDATE()) > 12 THEN ' PM'
    ELSE ' AM'
END) 'Hour:Minutes'