不太常用,对于一些IIF表达式,需要转换成sql server支持的格式,就写了这个转换。反复调试之后,就可以支持嵌套的调用了。
/**/
/// <summary>
/// 转换字符串中的IIF语句
/// </summary>
/// <param name="?"></param>
/// <returns></returns>
static
public
string
ChangeIIF(
string
oldstr)
![ExpandedBlockStart.gif](/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
string str=oldstr;
str.Trim();
int p0=oldstr.IndexOf("IIF");
if(p0==-1)return oldstr;
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
int p1=str.IndexOf("(",p0);
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
//排除中间的()的干扰
int l=0; //记录经过的(
int p2=p1+1;
for(;p2<str.Length ;p2++)
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if(l==0&&str.Substring(p2,1)==",")break;
if(str.Substring(p2,1)=="(")l++; //每增加一个(,l加1
if(str.Substring(p2,1)==")")l--; //每增加一个(,l加1
}
if(p2==str.Length)return ""; //意外
//int p2=str.IndexOf(",",p1);
l=0; //记录经过的(
int p3=p2+1;
for(;p3<str.Length ;p3++)
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if(l==0&&str.Substring(p3,1)==",")break;
if(str.Substring(p3,1)=="(")l++; //每增加一个(,l加1
if(str.Substring(p3,1)==")")l--; //每增加一个(,l加1
}
if(p3==str.Length)return ""; //意外
//int p3=str.IndexOf(",",p2+1); //第二个“,”
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
//取得结束的),排除中间的()的干扰
l=0; //记录经过的(
int p4=p3+1;
for(;p4<str.Length ;p4++)
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if(l==0&&str.Substring(p4,1)==")")break;
if(str.Substring(p4,1)=="(")l++; //每增加一个(,l加1
if(str.Substring(p4,1)==")")l--; //每增加一个(,l加1
}
//int p4=str.Length-1;
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
if(p4==str.Length)return ""; //意外
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
if(p1*p2*p3*p4<=0)return oldstr;
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
return oldstr.Substring(0,p0)+"CASE WHEN "+str.Substring(p1+1,p2-p1-1)+" THEN "+str.Substring(p2+1,p3-p2-1)+" ELSE "+str.Substring(p3+1,p4-p3-1)+" END"+oldstr.Substring(p4+1);
}