String_Split可以在VIEW中使用吗?



我有一个表,多个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也分割这个,然后我使用哪里摆脱一切不是IP

数据例子
>MoreText10.10.10.10,10.10.10.11,10.10.10.12

您的查询在视图中工作正常,如下所示。注意,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

最新更新