我有一個表,在單列中用逗號分隔多個 IP 地址,我正在使用以下查詢,并且在選擇中使用時它可以完美運行,但是當我嘗試創建視圖時它不起作用,它給了我錯誤
無效的物件名稱“string_split”。
可以string_split在視圖中實際使用嗎?我可以使用 SP,但它確實有助于它的視圖,因為我需要對這個結果與其他視圖和一個視圖進行多次連接UNION ALL
SELECT
distinct [ColumnA]
,[ColumnB]
,cs.value as IPs
FROM [table] as A
cross apply string_split(
replace(
replace(
replace(
replace([value], '-', ',')
, ';', ',')
, '_', ',')
, ' ', ',')
, ',')cs
where A.[value] <> ''
and cs.value like '%.%.%.%' --and cs.value like '%host%'
order by 3
資料通常也有很多垃圾文本,比如系統或其他詞,所以我用替換將它們過濾掉,所以string_split也將它分開,然后我使用 where 來擺脫所有不是 IP 的東西
資料示例
| A列 | B列 | 知識產權 |
|---|---|---|
| 一些文本 | 更多文字 | 10.10.10.10,10.10.10.11,10.10.10.12 |
用作查詢時的結果示例
| A列 | B列 | 知識產權 |
|---|---|---|
| 一些文本 | 更多文字 | 10.10.10.10 |
| 一些文本 | 更多文字 | 10.10.10.11 |
| 一些文本 | 更多文字 | 10.10.10.12 |
uj5u.com熱心網友回復:
您的查詢在視圖中作業正常,如下所示。請注意,SQL Server 2017 引入了Translate,您可以使用它代替嵌套的 Replace 函式
create or alter view test as
select ColumnA, ColumnB, cs.[value] as IPs
from t
cross apply string_split( translate(IPs, '-;_ ', ',,,,'),',')cs
where cs.[value] <> ''
and cs.value like '%.%.%.%'
GO
select * from test
order by IPs
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/316306.html
標籤:sql sql-server 查询语句 分裂 看法
上一篇:SASEG中用于獲取資料集上個月的WHERE陳述句?
下一篇:查詢以使用進入條件填充排行榜
