IIF statement in SQL Server 2005

前提是你 提交于 2019-12-07 03:10:05

问题


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?


回答1:


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.




回答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.




回答3:


   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%]


来源:https://stackoverflow.com/questions/4374907/iif-statement-in-sql-server-2005

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!