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


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

            static public string ChangeIIF(string oldstr)
            
    {
                
    string str=oldstr;
                str.Trim();
                
    int p0=oldstr.IndexOf("IIF");
                
    if(p0==-1)return oldstr;

                
    int p1=str.IndexOf("(",p0);

                
    //排除中间的()的干扰
                int l=0;                        //记录经过的(
                int p2=p1+1;
                
    for(;p2<str.Length ;p2++)
                
    {
                    
    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++)
                
    {
                    
    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);    //第二个“,”

                
    //取得结束的),排除中间的()的干扰
                l=0;                        //记录经过的(
                int p4=p3+1;
                
    for(;p4<str.Length ;p4++)
                
    {
                    
    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;

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

                
    if(p1*p2*p3*p4<=0)return oldstr;

                
    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);
            }
  • 相关阅读:
    springmvc spring mybatis框架整合
    多线程bug修复
    OutOfMemory
    CSS中强悍的相对单位之em(em-and-elastic-layouts)学习小记
    css中line-height行高的深入学习
    HTML5的新语义化的标签
    关于Three.js基本几何形状
    【Google Chrome】 Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, https, chrome-extension-resource问题解决
    模拟Bootstrap响应式网格系统
    关于我对写博客那些事儿的实用心得
  • 原文地址:https://www.cnblogs.com/jetz/p/243925.html
Copyright © 2020-2023  润新知