查询以从列获取特定模式

时间:2015-07-27 12:53:14

标签: sql plsql

我的一个表格列包含以下值。

All Files (*.*)|*.*|Bitmap (*.bmp)|*.bmp|Microsoft Word Document
(*.docx)|*.docx|GIF (*.gif)|*.gif|JPEG (*.jpg)|*.jpg|PNG
(*.png)|*.png|Adobe Reader (*.pdf)|*.pdf|TIFF (*.tif)|*.tif

我需要一个查询,它将从上面的值中获取bmp,docx,gif,jpg,png,pdf,tif。这些值显示在括号中。

2 个答案:

答案 0 :(得分:0)

这是你想要的吗?

SELECT * 
FROM your_table 
WHERE column_with_types IN ('(.bmp)','(.docx)','(.gif)','(.jpg)','(.png)','(.pdf)','(.tif)')

答案 1 :(得分:0)

好的,考虑到这些假设:

  • 您的样本数据为3行
  • 整行是一列
  • 您不希望第一行中的*.*

试试这个:

SQL> with tbl(line_nbr, str) as (
      select 1, 'All Files (*.*)|*.*|Bitmap (*.bmp)|*.bmp|Microsoft Word Document' from dual
      union
      select 2, '(*.docx)|*.docx|GIF (*.gif)|*.gif|JPEG (*.jpg)|*.jpg|PNG' from dual
      union
      select 3, '(*.png)|*.png|Adobe Reader (*.pdf)|*.pdf|TIFF (*.tif)|*.tif' from dual
    )
    SELECT line_nbr, str,
           COLUMN_VALUE AS match_nbr,
          REGEXP_SUBSTR( str ,'\(\*\.(\w*)\)', 1, COLUMN_VALUE, NULL, 1 ) AS match_value
   FROM   tbl,
          TABLE(
            CAST(
              MULTISET(
                SELECT LEVEL
                FROM   DUAL
                CONNECT BY LEVEL <= REGEXP_COUNT( str ,'\(\*\.\w*\)' )
              ) AS SYS.ODCINUMBERLIST
            )
          );

  LINE_NBR STR                                                               MATCH_NBR MATCH_VALUE
---------- ---------------------------------------------------------------- ---------- -----------
         1 All Files (*.*)|*.*|Bitmap (*.bmp)|*.bmp|Microsoft Word Document          1 bmp
         2 (*.docx)|*.docx|GIF (*.gif)|*.gif|JPEG (*.jpg)|*.jpg|PNG                  1 docx
         2 (*.docx)|*.docx|GIF (*.gif)|*.gif|JPEG (*.jpg)|*.jpg|PNG                  2 gif
         2 (*.docx)|*.docx|GIF (*.gif)|*.gif|JPEG (*.jpg)|*.jpg|PNG                  3 jpg
         3 (*.png)|*.png|Adobe Reader (*.pdf)|*.pdf|TIFF (*.tif)|*.tif               1 png
         3 (*.png)|*.png|Adobe Reader (*.pdf)|*.pdf|TIFF (*.tif)|*.tif               2 pdf
         3 (*.png)|*.png|Adobe Reader (*.pdf)|*.pdf|TIFF (*.tif)|*.tif               3 tif

7 rows selected.

SQL>

感谢MT0在这篇文章中提供了正确处理多行的技巧:Split string by space and character as delimiter in Oracle with regexp_substr