Wednesday, November 21, 2018

Replace a string in MySQL





MySQL Replace function to replace a string
REPLACE(str,old_string,new_string);
Example code in PHP:
  $sql = "UPDATE         courses_offered AS offered
    INNER JOIN courses_lut
        ON courses_lut.course_id = offered.course_id
    LEFT JOIN contract_si_ta_percourse AS SITA
        ON SITA.stakeholder_id = {$this->sanitizeInput($arr['stakeholder_id1'])}
            AND  SITA.Session0=offered.Session
        AND SITA.courses_id = offered.course_id AND SITA.semester = offered.Term   
        AND CASE
           WHEN LEFT(offered.Section ,2) ='OP'
         THEN  SITA.instr_section = offered.Section
          WHEN  TRIM(courses_lut.CourseSubject)='FASS' AND TRIM(courses_lut.CourseNumber)='101'  
        THEN  SITA.instr_section = offered.Section                     
          WHEN
          left(offered.Section, 1) in ('A', 'N') and right(offered.Section, 1) = '0'
            THEN
              SITA.instr_section = CONCAT(LEFT(offered.Section ,3),'0')
        ELSE SITA.instr_section = CONCAT(LEFT(offered.Section ,2),'00')
        END
        SET SITA.instr_section = REPLACE(SITA.instr_section, '$section1','$section2'),
          SITA.assign_section = REPLACE(SITA.assign_section, '$section1','$section2')

    WHERE offered.courses_offered_id = {$this->sanitizeInput($arr['courses_offered_id1'])}";   

        

No comments:

Post a Comment