Removing non-alphaNumerics in MySQL

后端 未结 5 516
既然无缘
既然无缘 2021-01-14 07:27

Do you know any easy way to remove (or replace) all non alphanumeric characters from varchar variable in Mysql?

something like String\'s replaceAll(\"[^a-zA-Z0-9]\",

5条回答
  •  别那么骄傲
    2021-01-14 07:34

    For MSSQL I have committed something like this:

    CREATE FUNCTION removeNonAlphaNum(@p_zthes VARCHAR(255)) 
    RETURNS varchar(255) 
    AS
    BEGIN
    DECLARE @v_bad_char_index INT;
    DECLARE @v_bad_char VARCHAR(1);
    DECLARE @v_res VARCHAR(255);
    SET @v_res = @p_zthes;
    SET @v_bad_char_index = patindex('%[^a-zA-Z0-9]%', @p_zthes) 
    WHILE (@v_bad_char_index > 0)
      BEGIN
        SET @v_bad_char = SUBSTRING(@p_zthes, @v_bad_char_index, 1);
        SET @v_res = REPLACE(@v_res, @v_bad_char, 'O');
        SET @v_bad_char_index = patindex('%[^a-zA-Z0-9]%', @v_res ) 
      END
    return @v_res;
    END 
    

    (but I am poor SQL programmer, so there probably exist some nicer solutions)

提交回复
热议问题