在 SQL 中将时间转换为 12 小时格式 [英] Converting a time into 12 hour format in SQL

查看:63
本文介绍了在 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.

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

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