'数据库用于存储大量数据'
我需要在上面的字符串中找到以元音开头的单词数。任何人都可以给我这样做的想法
答案 0 :(得分:3)
一种方法是使用常用的“拆分”用户定义函数,并使用它:
SELECT COUNT(*)
FROM dbo.fnSplit(‘Data base is used to store large amount of data’, ' ') x
WHERE x.Value LIKE '[aeiouAEIOU]%'
可以在此处找到拆分UDF的一些示例:T-SQL: Opposite to string concatenation - how to split string into multiple records
如果这是您正在运行的常见查询,我想我会考虑在将值插入表中并将数字存储在行中时计算出该计数。
答案 1 :(得分:1)
declare @str2 varchar(max)='Data base is used to store large amount of data'
Declare @data varchar(max)
Declare @Cnt int
Set @Cnt = 0
While (Charindex(' ',@str2)>0)
Begin
Select @Data = ltrim(rtrim(Substring(@str2,1,Charindex(' ',@str2)-1)))
Set @str2 = Substring(@str2,Charindex(' ',@str2)+1,len(@str2))
if(SUBSTRING(@Data,1,1)='a' or SUBSTRING(@Data,1,1)='e' or SUBSTRING(@Data,1,1)='i' or SUBSTRING(@Data,1,1)='o' or SUBSTRING(@Data,1,1)='u')
begin
Set @Cnt = @Cnt + 1
End
End
Select @Data = ltrim(rtrim(@str2))
if(SUBSTRING(@Data,1,1)='a' or SUBSTRING(@Data,1,1)='e' or SUBSTRING(@Data,1,1)='i' or SUBSTRING(@Data,1,1)='o' or SUBSTRING(@Data,1,1)='u')
begin
Set @Cnt = @Cnt + 1
End
select @Cnt
答案 2 :(得分:0)
declare @S varchar(100) = 'Data base is used to store large amount of data'
;with cte as
(
select case when upper(left(@S, 1)) in ('A','E','I','O','U') then 1 else 0 end as vow,
stuff(@S, 1, charindex(' ', @S+' '), '')+' ' as rest
where len(@S) > 0
union all
select case when upper(left(rest, 1)) in ('A','E','I','O','U') then 1 else 0 end as vow,
stuff(rest, 1, charindex(' ', rest), '') as rest
from cte
where len(rest) > 0
)
select coalesce(sum(vow), 0)
from cte
答案 3 :(得分:0)
OP提到在没有函数的情况下进行此操作,因此请尝试使用单个字符串(假设您有"Numbers"表):
--sp GEN_FN_GEN_ListToTable
DECLARE @String varchar(500)
,@SplitOn char(1)
SELECT @String='the quick red fox jumped over the lazy brown dog, again.'
,@SplitOn=' '
SELECT
COUNT(*) AS VowelCount
FROM (SELECT
LTRIM(RTRIM(SUBSTRING(List2, number+1, CHARINDEX(@SplitOn, List2, number+1)-number - 1))) AS ListValue
FROM (
SELECT @SplitOn + @String + @SplitOn AS List2
) AS dt
INNER JOIN Numbers n ON n.Number < LEN(dt.List2)
WHERE SUBSTRING(List2, number, 1) = @SplitOn
) dt2
WHERE ListValue IS NOT NULL AND ListValue!='' AND LEFT(ListValue,1) IN ('a','e','i','o','u')
输出:
VowelCount
-----------
2
(1 row(s) affected)
这里有一个如何计算表中许多字符串的元音(无函数)的例子(前提是你有一个"Numbers"表):
DECLARE @YourTable table (RowID int, RowValue varchar(500))
INSERT INTO @YourTable VALUES (1,'the quick red fox jumped over the lazy brown dog, again.')
INSERT INTO @YourTable VALUES (2,'just another example of some words.')
INSERT INTO @YourTable VALUES (3,'a b c d e f g h i j k l m n o p q r s t u v w x y z')
SELECT
RowID,COUNT(*) AS VowelCount
FROM (SELECT
RowID, LTRIM(RTRIM(SUBSTRING(List2, number+1, CHARINDEX(' ', List2, number+1)-number - 1))) AS ListValue
FROM (SELECT
RowID, ' ' + RowValue + ' ' AS List2
FROM @YourTable
) AS dt
INNER JOIN Numbers n ON n.Number < LEN(dt.List2)
WHERE SUBSTRING(List2, number, 1) = ' '
) dt2
WHERE ListValue IS NOT NULL AND ListValue!='' AND LEFT(ListValue,1) IN ('a','e','i','o','u')
GROUP BY RowID
输出:
RowID VowelCount
----------- -----------
1 2
2 3
3 5
(3 row(s) affected)
你基本上需要获得一个split函数,然后在一个空格here is a similar answer上拆分,它解释了一个split函数及其用法。我不知道为什么OP不想使用函数。
答案 4 :(得分:-1)
SELECT * FROM tbl_name WHERE col_name REGEXP 'vowels'