[英]T-SQL / Find all dots in a column
I have query like that :我有这样的查询:
DECLARE @TABLE TABLE (
ID int primary key identity(1,1),
CODE nvarchar(max)
);
INSERT INTO @TABLE VALUES ('320.01.001'),('320.01.002'),('320.001.002'),('320.01.002.0003.0002')
SELECT * FROM @TABLE
Result:结果:
I want to count of dots in a column .我想计算一列中的点数。
My excepted result:我的例外结果:
A pretty simple method is:一个非常简单的方法是:
select t.*, s.num_dots
from @table t cross apply
(select count(*) - 1 as num_dots
from string_split(t.code, '.') s
) s;
A more traditional method uses the difference between the lengths of two strings:更传统的方法使用两个字符串的长度之间的差异:
select t.*,
len(t.code) - len(replace(t.code, '.', '')) as num_dots
from @table t;
I actually do not have a sense of which of these is faster.我实际上不知道其中哪一个更快。 If I had to guess, I would guess the second, but if performance is an issue, you should test the two versions.如果我不得不猜测,我会猜测第二个,但如果性能是一个问题,你应该测试两个版本。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.