下面是我的資料如何出現在我的資料庫中的一個表中
| ID | 第 1 列 |
|---|---|
| 1 | 1234-6 1 |
| 2 | 123461 |
| 3 | 023-FD-0 |
| 4 | 023FD0 |
| 5 | 5FD23FO |
| 6 | 5FD 23F O |
| 7 | 12334 |
我如何在我的選擇陳述句中實作以下目標。
| ID | 第一列 | 列2 |
|---|---|---|
| 1 | 123461 | 1234-6 1 |
| 2 | 023FD0 | 023-FD-0 |
| 3 | 5FD23FO | 5FD 23F O |
曾嘗試使用正則運算式但徒勞無功。示例選擇查詢將不勝感激。
uj5u.com熱心網友回復:
如果您只想替換字符“-”和“ ”,您可以使用替換功能兩次。使用簡單的正則運算式來標識要處理的行:
with test(id, column1) as
(values (1, '1234-6 1')
, (2, '123461')
, (3, '023-fd-0')
, (4, '023fd0')
, (5, '5fd23fo')
, (6, '5fd 23f o')
, (7, '12334')
)
select id
, replace(replace(column1,'-',''),' ','') as column1
, column1 as column2
from test
where column1 ~ '(-|\ )';
uj5u.com熱心網友回復:
您可以使用 self- join:
select distinct case when t1.column1 < t2.column1 then t1.column1 else t2.column1 end,
case when t2.column1 > t1.column1 then t2.column1 else t1.column1 end from t t1
join t t2
on regexp_replace(t1.column1, '\W', '', 'g') = regexp_replace(t2.column1, '\W', '', 'g') and t1.id != t2.id
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/348649.html
標籤:sql PostgreSQL
