前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >一个可以解析嵌套IIF语句的代码

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

作者头像
用户1075292
发布2018-01-23 10:13:51
8980
发布2018-01-23 10:13:51
举报
文章被收录于专栏:听雨堂听雨堂

不太常用,对于一些IIF表达式,需要转换成sql server支持的格式,就写了这个转换。反复调试之后,就可以支持嵌套的调用了。

代码语言:javascript
复制
/// <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);
        }
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2005-09-26 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档