SQL Server 中的时间格式 [英] time format in SQL Server

查看:30
本文介绍了SQL Server 中的时间格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有谁知道如何将 select 语句的日期时间值格式化为仅在 SQL Server 中显示时间?

Does anyone know how can I format a select statement datetime value to only display time in SQL Server?

示例:

Table cuatomer
id   name   datetime
1    Alvin  2010-10-15 15:12:54:00
2    Ken    2010-10-08 09:23:56:00

当我选择我喜欢的表格时,结果将显示如下

When I select the table I like the result will display as below

id   name    time
1    Alvin   3:12PM
2    Ken     9:23AM

有什么办法可以在 mssql 中做到吗?

Any way that I can do it in mssql?

推荐答案

您可以组合使用 CONVERT、RIGHT 和 TRIM 来获得所需的结果:

You can use a combination of CONVERT, RIGHT and TRIM to get the desired result:

SELECT ltrim(right(convert(varchar(25), getdate(), 100), 7))

您在函数中看到的 100 指定了日期格式 mon dd yyyy hh:miAM(或 PM),然后我们就可以从中获取正确的字符.

The 100 you see in the function specifies the date format mon dd yyyy hh:miAM (or PM), and from there we just grab the right characters.

您可以在此处查看有关转换日期时间的更多信息.

You can see more about converting datetimes here.

这篇关于SQL Server 中的时间格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆