2010-10-28 3 views
0

Je dois obtenir les mots dans un champ de texte et de faire des mises à jour avec ces mots, par exemple:Comment obtenir des mots de champ dans SQL Server 2008

données originales

words  | other field | another field 
--------------------------------------------- 
white  |    | 
some words |    | 
some other w |    | 

résultat désiré

words  | other field | another field 
--------------------------------------------- 
white  |    | 
some   | words   | 
some   | other   | w 

Comment puis-je faire cela?

EXTRA

J'ai cette requête où je reçois combien de mots un champ ont

select nombre, 
     LEN(words) - LEN(REPLACE(words, ' ', ''))+1 as palabras 
    from origen_informacion 
where words <> '' 
+1

Alors, quelle est votre sortie désirée? 'white',' some', 'mots'? –

+0

Je dois effectuer une mise à jour pour chaque ligne avec les mots. J'ai juste besoin d'un conseil sur la façon de le faire. –

+0

Vous allez devoir clarifier ce que vous faites. Pouvez-vous nous montrer à quoi ressemblera table1 après votre mise à jour? –

Répondre

1

Dans SQL Server 2008, vous pouvez utiliser sys.dm_fts_parser pour diviser une chaîne en mots. Vous pouvez l'incorporer avec cross apply.

DECLARE @data TABLE 
(
id INT IDENTITY(1,1) PRIMARY KEY, 
words VARCHAR(1000), 
other_field VARCHAR(1000), 
another_field VARCHAR(1000) 
) 

INSERT INTO @data (words) 
VALUES ('white'),('some words'),('some other w '),('This sentence has 5 words'); 

WITH splitData AS 
(
SELECT 
     id , 
     max(case when occurrence = 1 then display_term end) as word1, 
     max(case when occurrence = 2 then display_term end) as word2, 
     max(case when occurrence = 3 then display_term end) as word3, 
     max(case when occurrence = 4 then display_term end) as word4  
FROM @data 
CROSS APPLY sys.dm_fts_parser('"' + REPLACE(words,'"','') + '"',1033,NULL,0) 
GROUP BY id 
HAVING MAX(occurrence) <= 4 
) 
UPDATE @data 
SET words = word1, other_field=word2, another_field=word3 + ISNULL(' ' + word4,'') 
FROM @data d1 
JOIN splitData sd ON d1.id = sd.id 

SELECT * FROM @data 

Sortie

id  words       other_field  another_field 
------ ------------------------------ --------------- -------------- 
1  white       NULL   NULL 
2  some       words   NULL 
3  some       other   w 
4  This sentence has 5 words  NULL   NULL 
2

Si vous essayez de diviser un espace chaîne séparée vous pouvez utiliser cette fonction:

create function fn_ParseCSVString 
(
@CSVString varchar(8000) , 
@Delimiter varchar(10) 
) 
returns @tbl table (s varchar(1000)) 
as 
/* 
select * from dbo.fn_ParseCSVString ('qwe rew wer', ',c,') 
*/ 
begin 
declare @i int , 
    @j int 
    select @i = 1 
    while @i <= len(@CSVString) 
    begin 
     select @j = charindex(@Delimiter, @CSVString, @i) 
     if @j = 0 
     begin 
      select @j = len(@CSVString) + 1 
     end 
     insert @tbl select substring(@CSVString, @i, @j - @i) 
     select @i = @j + len(@Delimiter) 
    end 
    return 
end 


GO 

Et passez '' comme délimiteur.

select * from dbo.fn_ParseCSVString ('qwe rew wer', ' ') 
+0

Merci Abe, je pense que cette fonction pourrait m'aider. –

Questions connexes