【问题标题】:SQL Format('MMMM') does not work in SQL Server 2008SQL 格式('MMMM')在 SQL Server 2008 中不起作用
【发布时间】:2019-07-30 19:45:14
【问题描述】:

我有这个问题

select 
    format([time], 'MMMM') as 'Month', 
    count([time]) as 'Application Usage', 
    count([time]) as 'Application Usage' 
from 
    UserLogs UL 
where 
    [time] >= Dateadd(Month, Datediff(Month, 0, DATEADD(m, -6, current_timestamp)), 0) 
    and Ul.UserId = @user 
group by
    format([time],'MMMM')

但是,我使用 SQL Server 2008 迁移到服务器并收到此错误:

'format' 不是可识别的内置函数名

我可以用什么来代替这个格式化函数?

【问题讨论】:

  • 它是在 2012 年推出的。也许可以试试 Select datename(MONTH,GetDate())
  • 顺便说明一下:SQL Server 2008 和 2008 R2 目前不再提供扩展支持 - red-gate.com/simple-talk/sql/database-administration/… - 是时候升级了!
  • 有时无法选择何时升级....

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


【解决方案1】:

SQL Server 2008 不支持format()。这里有一种互惠关系——微软不再支持 SQL Server 2008。

无论如何,只要使用datename()

select datename(month, [time]) as Month

【讨论】:

  • 我这样做了,它本身就可以工作,但是当我把它放在这样的查询中时 select (select datename(month, [time])) as 'Month', count([time]) as 'Application Usage', count([time]) as 'Application Usage' from UserLogs UL where [time] >= Dateadd(Month, Datediff(Month, 0, DATEADD(m, -6, current_timestamp)), 0) 和Ul.UserId = 1 它不起作用
  • 它说 'UserLogs.time' 列在选择列表中无效,因为它不包含在聚合函数或 GROUP BY 子句中。我放了一个 group by,它说我不能在 group by 子句中使用子查询
  • @unhandledExcepSean
  • @KevoyWalters 。 . .你需要一个GROUP BY
【解决方案2】:

这是 SQL Server 2012 及更高版本的一项功能。试试DATENAME(MONTH,[time]).

【讨论】:

  • select (select datename(month, [time])) as 'Month', count([time]) as 'Application Usage', count([time]) as 'Application Usage' from UserLogs UL where [time] >= Dateadd(Month, Datediff(Month, 0, DATEADD(m, -6, current_timestamp)), 0) and Ul.UserId = 1 给出此错误:Msg 8120, Level 16, State 1, Line 12 列 'UserLogs.time' 在选择列表中无效,因为它不包含在聚合函数或GROUP BY 子句。
  • @KevoyWalters 您是否将 GROUP BY 更新为 DATENAME(MONTH,[time])?
猜你喜欢
  • 1970-01-01
  • 2014-08-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-05-21
  • 1970-01-01
  • 2015-11-09
相关资源
最近更新 更多