在 SQL 中将时间转换为 12 小时格式

问题描述:

如何在 SQL Server 2008 中将 24 小时格式的时间转换为 12 小时格式的时间?

How can i convert 24 hour formatted time into 12 hour formatted time in SQL server 2008?

您是否在时间类型的变量/列中有当前时间?如果是这样,那就很简单了:

Do you have the current time in a variable/column of type time? If so, it's easy:

declare @t time
set @t = '14:40'

select CONVERT(varchar(15),@t,100)

结果:

2:40PM

如果它在 datetime(2) 变量中,那么您必须在运行 转换.如果是字符串,则先将其CONVERT转换为time,然后使用上面的代码.

If it's in a datetime(2) variable, then you'll have to strip out the date portion after running the CONVERT. If it's in a string, then first CONVERT it to time, then use the above code.