2
A
回答
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
-1
3
一種方法是使用常提到的「分裂」用戶自定義函數,並使用它像這樣:
SELECT COUNT(*)
FROM dbo.fnSplit(‘Data base is used to store large amount of data’, ' ') x
WHERE x.Value LIKE '[aeiouAEIOU]%'
的一些例子split UDF可以在這裏找到:T-SQL: Opposite to string concatenation - how to split string into multiple records
如果這是一個常見的查詢你正在運行,我想我會很想考慮計算值插入到表中e並將該號碼存儲在該行中。
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
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')
OUTPUT:
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
OUTPUT:
RowID VowelCount
----------- -----------
1 2
2 3
3 5
(3 row(s) affected)
你基本上需要得到一個分割功能,然後分裂的一個空間中,here is a similar answer這也解釋了關於分裂的功能和它的使用。我不確定OP爲什麼不想使用某個功能。
相關問題
- 1. 查找單詞中的元音數量
- 2. 如何用Python找出列表中以元音開頭的單詞?
- 3. 需要在單詞內找到元音
- 4. 在字符串的開頭找到元音和大寫char?
- 5. 如何找到以確切單詞開頭的字符串
- 6. 用preg_match在字符串的開頭找到整個單詞?
- 7. 查找以輔音開頭和結尾的單詞
- 8. sed - 打印僅以元音開頭的單詞
- 9. 找到以指定字符串開頭的單詞數量的最快方法(單詞存儲在AVL樹中)
- 10. 查找單詞中的音節數
- 11. ActiveRecord查找以數組中的單詞開頭的記錄
- 12. 檢查,如果一個詞以元音開頭或輔音
- 13. 在單詞中查找元音的函數?
- 14. 找出一個單詞是否可以成爲詞典中單詞的開頭
- 15. 計算字符串中某個單詞的元音數
- 16. 找到作爲迴文的第一個單詞的開頭
- 17. 查找以給定搜索字符串開頭的詞數 - Regex
- 18. 如何保留元音字母中所有單詞的開頭,但刪除字符串的其餘部分
- 19. 查找字符串中的單詞詞
- 20. 查找以列表中的給定字母開頭的單詞
- 21. 查找一個文件中開頭的行特定的單詞
- 22. 查找一個文件中以修復字符串開頭的所有單詞?
- 23. 以大寫重音(JAVA)開頭的單詞
- 24. 如何檢查單詞中的元音
- 25. php刪除單詞開頭#
- 26. Elasticsearch multi_match在單詞開頭
- 27. 找出單詞中第一個元音的出現位置
- 28. 如何在字符串中查找元音,並在屏幕上打印含有最多元音的單詞?
- 29. 找到字符串開頭的字符串數組內的PHP
- 30. 從字符串中提取以字母開頭的單詞
有沒有其他的方式做它沒有寫函數。我們通過循環字符串來做到這一點。 – Pearl
是的,檢查我的答案我編輯它 –
Tanq它的工作原理:-) – Pearl