【问题标题】:Format time using 12 hour notation使用 12 小时表示法格式化时间
【发布时间】:2020-12-25 10:37:48
【问题描述】:

我做了一个显示日期和时间的 SQL 查询,时间分为小时和分钟。

select convert(varchar(10),[Date],23) as [Date], 
datepart(hour, [Time])as Hour, datepart(minute, [Time])as Minutes, 
FROM [SQLIOT].[dbo].[ZEPB_CaseLog] 

表格以 24 小时格式显示小时。 但我想改为 12 小时格式,单个数字带有“0”前缀。

即:01、02、03 等

我想过用case来做:

select convert(varchar(10),[Date],23) as [Date],
case when 
datepart(hour, [Time])> 12 then (datepart(hour, [Time])- 12) as Hour
, datepart(minute, [Time])as Minutes, 
FROM [SQLIOT].[dbo].[ZEPB_CaseLog] 

这样做会给我一个在线语法错误

datepart(hour, [Time])> 12 then (datepart(hour, [Time])- 12) as Hour

--Incorrect syntax near the keyword 'as'.

我对在 SQL 本身中做减法不太熟悉。我还应该添加什么来解决这个问题吗?

【问题讨论】:

  • format([Time],'HH')?
  • 当涉及到日期时间时,您需要使用相关函数,这些函数都有很好的文档记录 - 如果您确实需要添加/减去小时数,您可以使用 dateadd,但它只是格式化, 使用format
  • 我尝试了你的建议,但 SQL 给了我错误提示 'format' is not a recognized built-in function name.
  • SQL Server 2008
  • wooo - 不支持 - 升级时间

标签: sql sql-server sql-server-2008


【解决方案1】:

case when 存在语法错误,请尝试以下操作。

select convert(varchar(10),[Date],23) as [Date]
    , case when datepart(hour, [Time])> 12
    then (datepart(hour, [Time])- 12) 
    else datepart(hour, [Time]) End as Hour
    , datepart(minute, [Time])as Minutes, 
FROM [SQLIOT].[dbo].[ZEPB_CaseLog] 

【讨论】:

【解决方案2】:

如果您对格式感兴趣,即显示,那么让我们使用字符串。

declare @Time time = '09:33:44 pm'

select @Time [Original]
  , format(cast(@Time as datetime),N'hh') [Format Hour] -- SQL Server 2012 + (format works differently for time and datetime - datetime is required here)
  , case when len(convert(varchar(32),@Time,109)) > 17 then convert(varchar(2),@Time,109) else '0' + convert(varchar(1),@Time,109) end [Convert Hour] -- SQL Server pre-2012
  -- If you ultimatly want the hours and minutes together then do it as one
  , format(cast(@Time as datetime),N'hh\:mm') [Format hour:min] -- SQL Server 2012 + (format works differently for time and datetime - datetime is required here)
  , case when len(convert(varchar(32),@Time,109)) > 17 then convert(varchar(5),@Time,109) else '0' + convert(varchar(4),@Time,109) end [Convert hour:min] -- SQL Server pre-2012
  -- And if you want am/pm
  , format(cast(@Time as datetime),N'hh\:mm tt') [Format hour:min tt] -- SQL Server 2012 + (format works differently for time and datetime - datetime is required here)
  , case when len(convert(varchar(32),@Time,109)) > 17 then convert(varchar(5),@Time,109) else '0' + convert(varchar(4),@Time,109) end -- SQL Server pre-2012
  + case when @Time >= '12:00:00' then ' PM' else ' AM' end [Convert hour:min tt] -- SQL Server pre-2012

返回:

Original            Format Hour Convert Hour    Format hour:min Convert hour:min    Format hour:min tt  Convert hour:min tt
21:33:44.0000000    09          09              09:33           09:33               09:33 PM            09:33 PM

如果您对添加/减去日期感兴趣,请使用dateadd

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-08-08
    • 2020-12-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多