簡體   English   中英

SQL 從字符串中刪除不需要的特殊字符

[英]SQL remove unwanted special characters from a string

嗨,我是 SQL 的新手,正在為一列等級值編寫案例說明。 這些值可以是 3 的長度,例如 A02、B04、A10、A09、D03。 第一個字符是字母,后兩個是數字。

如果用戶輸入'A02,我想將其更改為 A02。 如果存在,基本上刪除任何特殊字符。

CASE
WHEN Grade like '[^0-9A-z]%' THEN ''
else Grade end as Grade

到目前為止,我有這個,但我不知道如何使用正則表達式來刪除只搜索它的字符。

除非你真的想為它的樂趣做一個 CASE,在 oracle 我會這樣做,當你 select 它時,它會刪除標點符號和空格。 請注意,這不會驗證格式,因此會返回 Z1234 的成績。

WITH tbl(ID, grade) AS (
  SELECT 1, 'A01' FROM dual UNION ALL
  SELECT 1, '''B02' FROM dual UNION ALL
  SELECT 2, '$    C01&' FROM dual
)
SELECT ID, grade, REGEXP_REPLACE(grade, '([[:punct:]]| )') AS grade_scrubbed
from tbl;


        ID GRADE     GRADE_SCRUBBED
---------- --------- --------------
         1 A01       A01           
         1 'B02      B02           
         2 $    C01& C01           

3 rows selected.

但是,也就是說,由於您似乎想要驗證格式並使用正則表達式,您可以這樣做,盡管它有點笨拙。 看評論。

WITH tbl(ID, grade) AS (
  -- Test data. Include every crazy combo you'd never expect to see,
  --   because you WILL see it, it's just a matter of time :-)
  SELECT 1, 'A01'               FROM dual UNION ALL
  SELECT 1, '''B02'             FROM dual UNION ALL
  SELECT 2, '$    C01&'         FROM dual UNION ALL
  SELECT 3, 'DDD'               FROM dual UNION ALL
  SELECT 4, 'A'||CHR(10)||'DEF' FROM dual UNION ALL
  SELECT 5, 'Z1234'             FROM dual UNION ALL
  SELECT 6, NULL                FROM dual
)
SELECT ID, grade, 
CASE 
  -- Correct format of A99.
  WHEN REGEXP_LIKE(grade, '^[A-Z]\d{2}$') 
    THEN grade
  -- if not A99, see if stripping out punctuation and spaces make it match A99.
  --   If so, return with punctuation and spaces stripped out.
  WHEN NOT REGEXP_LIKE(grade, '^[A-Z]\d{2}$') 
    AND REGEXP_LIKE(REGEXP_REPLACE(grade, '([[:punct:]]| )'), '^[A-Z]\d{2}$')
    THEN REGEXP_REPLACE(grade, '([[:punct:]]| )')
  -- if not A99, and stripping out punctuation and spaces didn't make it match A99,
  --   then the grade is in the wrong format.
  WHEN NOT REGEXP_LIKE(grade, '^[A-Z]\d{2}$') 
    AND NOT REGEXP_LIKE(REGEXP_REPLACE(grade, '([[:punct:]]| )'), '^[A-Z]\d{2}$')
    THEN 'Invalid grade format'
  -- Something fell through all cases we tested for.  Always expect the unexpected!
  ELSE 'No case matched!'
END AS grade_scrubbed
from tbl;


        ID GRADE                GRADE_SCRUBBED      
---------- -------------------- --------------------
         1 A01                  A01                 
         1 'B02                 B02                 
         2 $    C01&            C01                 
         3 DDD                  Invalid grade format
         4 A
DEF                            Invalid grade format
                                                    
                                                                                
         5 Z1234                Invalid grade format
         6                      No case matched!    

7 rows selected.

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM