How to get time part from SQL Server 2005 datetime

2020-02-10 02:56发布

How to get time part from SQL Server 2005 datetime in HH:mm tt format

E.g.

11:25 AM
14:36 PM

9条回答
一纸荒年 Trace。
2楼-- · 2020-02-10 03:08

You'll need two converts, one to get the HH:mm time, and one to get AM/PM. For example:

declare @date datetime
set @date = '20:01'
SELECT CONVERT(VARCHAR(5), @date, 108) + ' ' +
       SUBSTRING(CONVERT(VARCHAR(19), @date, 100),18,2)

This prints:

20:01 PM

In a select query, replace @date with your column's name.

查看更多
老娘就宠你
3楼-- · 2020-02-10 03:09

You need to use CONVERT function:

CONVERT(VARCHAR, yourdatetimefiled, 114) AS [HH:MI(12H)]
查看更多
别忘想泡老子
4楼-- · 2020-02-10 03:10

One way is:

SELECT LTRIM(RIGHT(CONVERT(VARCHAR(20), GETDATE(), 100), 7))

If you have a look at Books Online here, format 100 is the one that has the time element in the format you want it in, it's just a case of stripping off the date from the front.

查看更多
ら.Afraid
5楼-- · 2020-02-10 03:11
select substring(CONVERT(VARCHAR, getdate(), 114),1,5)

resault : 22:05

查看更多
爱情/是我丢掉的垃圾
6楼-- · 2020-02-10 03:15
select right(convert(char(20),getdate(),0),7)

No check though

查看更多
一夜七次
7楼-- · 2020-02-10 03:16

This gives you an actual datetime and not varchar

CAST(LEFT(YOURDATETIME,12) AS SMALLDATETIME) AS YOURNEWDATE
查看更多
登录 后发表回答