2010-05-05 108 views
0

列詞數字的值,WordsCountSQL Server 2008的更新一列與基於在其他

對於具有如圖3所示,「AB」字「XYZ」更新wordscount的每個值與2,「ABCD」與4-等...

我該怎麼做有/沒有.NET程序?

+0

問題是沒有意義的因爲... – 2010-05-05 02:08:48

+0

更新了詞語「how」。 – MicMit 2010-05-05 02:27:53

回答

2

如果你可以總是被一個空格隔開的所有詞數:

UPDATE table SET WordCount = (LEN(Words) - LEN(REPLACE(Words, ' ', ''))) + 1 

這打破的時候沒有什麼或NULL的話,但你的想法..

1

您也許能在那裏你在一個空間分割分割功能來做到這一點:

Create FUNCTION [dbo].[udf_Split] (@DelimitedList nvarchar(max), @Delimiter nvarchar(2) = ',') 
RETURNS @SplitResults TABLE (Position int NOT NULL PRIMARY KEY, Value nvarchar(max)) 
AS 
Begin 
    Declare @DelimiterLength int 
    Set @DelimiterLength = DataLength(@Delimiter)/2 

    If Left(@DelimitedList, @DelimiterLength) <> @Delimiter 
     Set @DelimitedList = @Delimiter + @DelimitedList 

    If Right(@DelimitedList, @DelimiterLength) <> @Delimiter 
     Set @DelimitedList = @DelimitedList + @Delimiter 

    Insert @SplitResults(Position, Value) 
    Select CharIndex(@Delimiter, A.list, N.Value) + @DelimiterLength    
     , Substring (
        A.List 
        , CharIndex(@Delimiter, A.list, N.Value) + @DelimiterLength   
        , CharIndex(@Delimiter, A.list, N.Value + 1)        
         - (CharIndex(@Delimiter, A.list, N.Value) + @DelimiterLength) 
        ) 
    From dbo.Numbers As N 
     Cross Join (Select @DelimitedList As list) As A 
    Where N.Value > 0 
     And N.Value < LEN(A.list) 
     And Substring(A.list, N.Value, @DelimiterLength) = @Delimiter 
    Order By N.Value 

    Return 
End 

Update Table 
Set WordCount = (
       Select Count(*) 
       From dbo.udf_Split(Table.WordlistCol, ' ') 
       ) 

這是一個分裂功能的版本依賴於它包含整數的順序列表Numbers表的存在值。

相關問題