Code that can parse nested IIF statements

Source: Internet
Author: User
This conversion is not very common. For some IIF expressions, You need to convert them to the formats supported by SQL Server. After debugging, you can support nested calls. /**/ ///   <Summary>
/// IIF statement in the conversion string
///   </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 );

// Eliminate intermediate () Interference
Int L = 0 ; // Recorded (
Int P2 = P1 + 1 ;
For (; P2 < Str. length; p2 ++ )
{
If (L = 0 && Str. substring (P2, 1 ) = " , " ) Break ;

If (Str. substring (P2, 1 ) = " ( " ) L ++ ; // Each time one (, l plus 1) is added
If (Str. substring (P2, 1 ) = " ) " ) L -- ; // Each time one (, l plus 1) is added
}
If (P2 = Str. length) Return   "" ; // Unexpected
// Int P2 = Str. indexof (",", P1 );

L = 0 ; // Recorded (
Int P3 = P2 + 1 ;
For (; P3 < Str. length; p3 ++ )
{
If (L = 0 && Str. substring (P3, 1 ) = " , " ) Break ;

If (Str. substring (P3, 1 ) = " ( " ) L ++ ; // Each time one (, l plus 1) is added
If (Str. substring (P3, 1 ) = " ) " ) L -- ; // Each time one (, l plus 1) is added
}
If (P3 = Str. length) Return   "" ; // Unexpected
// Int P3 = Str. indexof (",", p2 + 1 ); // The second ","

// To eliminate the interference ().
L = 0 ; // Recorded (
Int P4 = P3 + 1 ;
For (; P4 < Str. length; P4 ++ )
{
If (L = 0 && Str. substring (P4, 1 ) = " ) " ) Break ;

If (Str. substring (P4, 1 ) = " ( " ) L ++ ; // Each time one (, l plus 1) is added
If (Str. substring (P4, 1 ) = " ) " ) L -- ; // Each time one (, l plus 1) is added
}
// Int P4 = Str. Length-1;

If (P4 = Str. length) Return   "" ; // Unexpected

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 );
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.