如何從列中僅提取編號的行 (How to extract only numbered rows from a column)


問題描述

如何從列中僅提取編號的行 (How to extract only numbered rows from a column)

For e.g: I have a column named "Card No", its a varchar but majority of the rows contain numbers and few contains alphanumeric numbers. I want to ignore all the records that are not numbers. 

Below are a few examples for the same:

Select CardNo from XX;

Result:

CardNo:
_______
1234,
5467,
1234/5467,
abc 667,
efg428

In the above result set, I want to eliminate the last three values. Please advise.


參考解法

方法 1:

Select CardNo from XX
where translate(CardNo, ' 0123456789', ' ') is null

方法 2:

Oracle 10+ has a regular expression system that you could use. Just so you know, no indexes can or will be used for this type of comparison. Oracle will need to look at every single row.

Take a look at http://docs.oracle.com/cd/B12037_01/server.101/b10759/functions114.htm#SQLRF06300

Something like:

WHERE REGEXP_INSTR('CardNo', '[^0‑9]+') > 0

方法 3:

Using regular expressions is probably the best choice here, but if you don't feel like going that route you could do something like this:

select CLEANED_CARDNO
  from (select CARDNO CLEANED_CARDNO,
               replace(TRANSLATE(CARDNO, '0123456789/', '~'), '~', null) STATUS
          from (select CARDNO from XX))
 where STATUS is null;

Changing the second argument in the TRANSLATE() function will allow you to include whatever characters you want.

方法 4:

Select CardNo from XX where isNumeric(CardNo) = 1

(by AdharshEgor SkriptunoffgviewKylePurdy RA)

參考文件

  1. How to extract only numbered rows from a column (CC BY‑SA 3.0/4.0)

#plsql #SQL #chars






相關問題

RAISE_APPLICATION_ERROR 不返回消息 (RAISE_APPLICATION_ERROR doesn't return the message)

PL/SQL 塊和循環練習 (PL/SQL block and LOOP exercise)

如何從列中僅提取編號的行 (How to extract only numbered rows from a column)

如何編寫一個程序來自動執行一組查詢 (How to write a procedure to execute set of queries automatically)

使用 DBMS_OUTPUT.put_line 顯示錯誤消息 (Display error message using DBMS_OUTPUT.put_line)

如何編寫將Oracle數據庫表數據導出到excel文件的程序 (How to write a Procedure which exports Oracle database table data into excel file)

如何重載對像類型中的方法 (How to overload a method in an object type)

有沒有辦法以編程方式從 Oracle 包中提取表引用? (Is there a way to programmatically extract table references from an Oracle package?)

批量插入 Oracle 數據庫:哪個更好:FOR 游標循環還是簡單的選擇? (Bulk Insert into Oracle database: Which is better: FOR Cursor loop or a simple Select?)

如何使用參數“foo 表”執行 SP? (How to execute SP with arguments 'table of foo'?)

PL/SQL 我做錯了什麼? (PL/SQL What am I doing wrong?)

PL/SQL 顯示帶有條件的表中的數據 (PL/SQL Display Data From a Table with a Condition)







留言討論