一个可以解析嵌套IIF语句的代码

不太常用,对于一些IIF表达式,需要转换成sql server支持的格式,就写了这个转换。反复调试之后,就可以支持嵌套的调用了。
ExpandedBlockStart.gif ContractedBlock.gif          /**/ /// <summary>
InBlock.gif        
/// 转换字符串中的IIF语句
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="?"></param>
ExpandedBlockEnd.gif        
/// <returns></returns>

None.gif          static   public   string  ChangeIIF( string  oldstr)
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif {
InBlock.gif            
string str=oldstr;
InBlock.gif            str.Trim();
InBlock.gif            
int p0=oldstr.IndexOf("IIF");
InBlock.gif            
if(p0==-1)return oldstr;
InBlock.gif
InBlock.gif            
int p1=str.IndexOf("(",p0);
InBlock.gif
InBlock.gif            
//排除中间的()的干扰
InBlock.gif
            int l=0;                        //记录经过的(
InBlock.gif
            int p2=p1+1;
InBlock.gif            
for(;p2<str.Length ;p2++)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
if(l==0&&str.Substring(p2,1)==",")break;
InBlock.gif                
InBlock.gif                
if(str.Substring(p2,1)=="(")l++;    //每增加一个(,l加1
InBlock.gif
                if(str.Substring(p2,1)==")")l--;    //每增加一个(,l加1
ExpandedSubBlockEnd.gif
            }

InBlock.gif            
if(p2==str.Length)return "";        //意外
InBlock.gif            
//int p2=str.IndexOf(",",p1);
InBlock.gif
            
InBlock.gif            l
=0;                        //记录经过的(
InBlock.gif
            int p3=p2+1;
InBlock.gif            
for(;p3<str.Length ;p3++)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
if(l==0&&str.Substring(p3,1)==",")break;
InBlock.gif                
InBlock.gif                
if(str.Substring(p3,1)=="(")l++;    //每增加一个(,l加1
InBlock.gif
                if(str.Substring(p3,1)==")")l--;    //每增加一个(,l加1
ExpandedSubBlockEnd.gif
            }

InBlock.gif            
if(p3==str.Length)return "";        //意外
InBlock.gif            
//int p3=str.IndexOf(",",p2+1);    //第二个“,”
InBlock.gif
InBlock.gif            
//取得结束的),排除中间的()的干扰
InBlock.gif
            l=0;                        //记录经过的(
InBlock.gif
            int p4=p3+1;
InBlock.gif            
for(;p4<str.Length ;p4++)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
if(l==0&&str.Substring(p4,1)==")")break;
InBlock.gif                
InBlock.gif                
if(str.Substring(p4,1)=="(")l++;    //每增加一个(,l加1
InBlock.gif
                if(str.Substring(p4,1)==")")l--;    //每增加一个(,l加1
ExpandedSubBlockEnd.gif
            }

InBlock.gif            
//int p4=str.Length-1;
InBlock.gif

InBlock.gif            
if(p4==str.Length)return "";        //意外
InBlock.gif

InBlock.gif            
if(p1*p2*p3*p4<=0)return oldstr;
InBlock.gif
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);
ExpandedBlockEnd.gif        }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值