子串MySQL

时间:2015-09-09 14:53:25

标签: mysql regex position substring locate

我需要从审计/日志表中删除SSN,我们无法更改应用程序。

应用程序生成审计/登录表'redcap_log_event',列'sql_log'。此列只是应用程序在应用程序中执行每个命令时使用的一长串SQL。

这是sql_log列的一个示例,当您复制并粘贴它时,回车符在列的SQL字符串中。插入最终位于应用程序的sql_log列中 -

INSERT INTO redcap_data (project_id, event_id, record, field_name, value, instance) VALUES (6, 21, 'Ashby, zbn', 'study_id', '123456789', NULL);
 INSERT INTO redcap_data (project_id, event_id, record, field_name, value, instance) VALUES (6, 21, 'Ashby, zbn', 'cmr_date', '2015-09-02', NULL);
 INSERT INTO redcap_data (project_id, event_id, record, field_name, value, instance) VALUES (6, 21, 'Ashby, zbn', 'first_name', 'bnm', NULL);
 INSERT INTO redcap_data (project_id, event_id, record, field_name, value, instance) VALUES (6, 21, 'Ashby, zbn', 'last_name', 'asdfggh', NULL);
 INSERT INTO redcap_data (project_id, event_id, record, field_name, value, instance) VALUES (6, 21, 'Ashby, zbn', 'dob', '2015-09-02', NULL);
 INSERT INTO redcap_data (project_id, event_id, record, field_name, value, instance) VALUES (6, 21, 'Ashby, zbn', 'age_at_cmr', '21', NULL);
 INSERT INTO redcap_data (project_id, event_id, record, field_name, value, instance) VALUES (6, 21, 'Ashby, zbn', 'social_security_number', '987582154', NULL);
 INSERT INTO redcap_data (project_id, event_id, record, field_name, value, instance) VALUES (6, 21, 'Ashby, zbn', 'sex', '1', NULL);
 INSERT INTO redcap_data (project_id, event_id, record, field_name, value, instance) VALUES (6, 21, 'Ashby, zbn', 'ethnicity', '1', NULL);
 INSERT INTO redcap_data (project_id, event_id, record, field_name, value, instance) VALUES (6, 21, 'Ashby, zbn', 'demographics_abef_complete', '2', NULL);
 INSERT INTO redcap_data (project_id, event_id, record, field_name, value, instance) VALUES (6, 21, 'Ashby, zbn', 'participant_id', 'asdfggh, zbn', NULL)

我想最终通过桌面上的触发器从上面的数据中删除9位SSN值,但是现在我正在努力获得子串,正则表达式,找到工作我需要它的方式 - 列sql_log中文本'social_security_number'的位置将始终不同,因为在它之前插入了firstname和lastname,所以我需要以某种方式在'social_security_number'的右侧找到不同的位置

任何想法都赞赏!

1 个答案:

答案 0 :(得分:0)

感谢我们看到的友好社区。无论如何,这就是我完成的: -

USE `ctsiredcap`;
DROP procedure IF EXISTS `SsnBlankOutInsert`;

DELIMITER $$
USE `ctsiredcap`$$
CREATE PROCEDURE `SsnBlankOutInsert` ()
BEGIN
UPDATE redcap_log_event 
SET 
    sql_log = CONCAT(SUBSTR(sql_log,
                1,
                LOCATE('social_security_number', sql_log) + 25),
            '*********',
            SUBSTR(sql_log,
                LOCATE('social_security_number', sql_log) + 35)),
    data_values = CONCAT(SUBSTR(data_values,
                1,
                LOCATE('social_security_number', data_values) + 25),
            '*********',
            SUBSTR(data_values,
                LOCATE('social_security_number', data_values) + 35))
WHERE
    ts > REPLACE(REPLACE(REPLACE(NOW() - INTERVAL 10 MINUTE,
                '-',
                ''),
            ':',
            ''),
        ' ',
        '')
        AND LOCATE('social_security_number', sql_log) > 0;
END$$

DELIMITER ;

通过任何方式都不漂亮,但似乎正在做这项工作

谢谢, 艾伦