SQL Server 2005 中的 IIF 语句

发布于 2024-10-06 13:13:54 字数 456 浏览 0 评论 0原文

IIF 语句是否存在于所有版本的 SQL Server 中?

我已经检查了 MSDN 上的教程。

但是当我尝试在我的机器上运行此代码时

DECLARE @newDate datetime
SET @newDate =  CONVERT(varchar, {fn NOW()}, 111)
SELECT IIF(@newDate > '2010/12/2', 'Greater', 'smaller')

,我收到了“'>'附近的语法不正确”的错误。

有人可以为我提供一个 SQL Server 2005 中存在 IIF 语句的示例吗?

Does IIF statement exists in all version of SQL Server ?

I have checked a tutorial on MSDN.

But when I tried to run this code on my machine

DECLARE @newDate datetime
SET @newDate =  CONVERT(varchar, {fn NOW()}, 111)
SELECT IIF(@newDate > '2010/12/2', 'Greater', 'smaller')

But I am getting error of "Incorrect syntax near '>'."

Can someone provide me an example in SQL Server 2005 for the existence of the IIF statement?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

梦太阳 2024-10-13 13:13:54

IIF 语句仅存在于 MDX 中 - SQL Server 分析 服务(SQL Server 的数据仓库端)的查询语言。

普通 T-SQL 没有IIF 语句。

在 T-SQL 中您能做的最好的事情就是使用 CASE.... WHEN... THEN... 语句。

That IIF statement only exists in MDX - the query language for SQL Server Analysis Services - the datawarehousing side of SQL Server.

Plain T-SQL does not have an IIF statement.

The best you can do in T-SQL is use the CASE.... WHEN... THEN... statement.

っ〆星空下的拥抱 2024-10-13 13:13:54

您最好使用 CASE 表达式:

DECLARE @newDate datetime
SET @newDate =  CONVERT(varchar, {fn NOW()}, 111)
SELECT CASE WHEN @newDate > '20101202' THEN 'Greater' ELSE 'smaller' END

另请注意,我已将日期文字转换为安全格式 - SQL Server 可以将“2010/12/2”解释为 2 月 12 日或 12 月 2 日。

You're better off using a CASE expression:

DECLARE @newDate datetime
SET @newDate =  CONVERT(varchar, {fn NOW()}, 111)
SELECT CASE WHEN @newDate > '20101202' THEN 'Greater' ELSE 'smaller' END

Please also note that I've switched your date literal to a safe format - '2010/12/2' could be interpreted by SQL server as either the 12th February or 2nd December.

墨小沫ゞ 2024-10-13 13:13:54
   IIF([Add DVP Month].DevelopmentMonth>[Add DVP Month].COVMONTHS,
       1,
   IIF([STATUS]<>'1',
       1, 
   IIF([Add DVP Month].PLANTYPE = 'A' and [Add DVP Month].newUsedProgram = 'U' and [Add DVP Month].COVMONTHS = 60 and [Add DVP Month].COVMILES = 100000 and [Add DVP Month].postedDt >= #1/31/2010#,
   IIF([Add DVP Month].postedDt >= #1/31/2012#, 
       [EPMthd.PCM2],   
   IIF([Add DVP Month].postedDt >= #1/31/2010#, 
   [EPMthd.PCM1], 
   [EPMthd.PCM0])
   ),
   IIF([Add DVP Month].COVMONTHS = 999,[EPMthd.2],
   IIF([Add DVP Month].postedDt >= #1/31/2012#, [EPMthd.2],
   IIF([Add DVP Month].postedDt >= #1/31/2010#, [EPMthd.1],
   IIF([Add DVP Month].postedDt >= #1/31/2008#, 
   IIF([EPMthd.0] is null, 
       [EPMthd.8], 
       [EPMthd.0]
      ),
   IIF([Add DVP Month].postedDt < #1/31/2008#, 
   IIF([EPMthd.8] is null, 
   IIF([Add DVP Month].COVMONTHS = 0,0, 
       [Add DVP Month].DevelopmentMonth/[Add DVP Month].COVMONTHS
      ),
       [EPMthd.8]
    ),
   IIF([Add DVP Month].COVMONTHS = 0,
   0, 
       [Add DVP Month].DevelopmentMonth/[Add DVP Month].COVMONTHS
    )
   )
 ))))))
 ) AS [EP%]
   IIF([Add DVP Month].DevelopmentMonth>[Add DVP Month].COVMONTHS,
       1,
   IIF([STATUS]<>'1',
       1, 
   IIF([Add DVP Month].PLANTYPE = 'A' and [Add DVP Month].newUsedProgram = 'U' and [Add DVP Month].COVMONTHS = 60 and [Add DVP Month].COVMILES = 100000 and [Add DVP Month].postedDt >= #1/31/2010#,
   IIF([Add DVP Month].postedDt >= #1/31/2012#, 
       [EPMthd.PCM2],   
   IIF([Add DVP Month].postedDt >= #1/31/2010#, 
   [EPMthd.PCM1], 
   [EPMthd.PCM0])
   ),
   IIF([Add DVP Month].COVMONTHS = 999,[EPMthd.2],
   IIF([Add DVP Month].postedDt >= #1/31/2012#, [EPMthd.2],
   IIF([Add DVP Month].postedDt >= #1/31/2010#, [EPMthd.1],
   IIF([Add DVP Month].postedDt >= #1/31/2008#, 
   IIF([EPMthd.0] is null, 
       [EPMthd.8], 
       [EPMthd.0]
      ),
   IIF([Add DVP Month].postedDt < #1/31/2008#, 
   IIF([EPMthd.8] is null, 
   IIF([Add DVP Month].COVMONTHS = 0,0, 
       [Add DVP Month].DevelopmentMonth/[Add DVP Month].COVMONTHS
      ),
       [EPMthd.8]
    ),
   IIF([Add DVP Month].COVMONTHS = 0,
   0, 
       [Add DVP Month].DevelopmentMonth/[Add DVP Month].COVMONTHS
    )
   )
 ))))))
 ) AS [EP%]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文