开发者

How to get words from field in SQL Server 2008

开发者 https://www.devze.com 2023-01-23 01:17 出处:网络
I need to get the words in a text field and make some updates with those words, for example: original data

I need to get the words in a text field and make some updates with those words, for example:

original data

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

desired result

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

How can I get this done?

EXTRA

I have this query wh开发者_如何学JAVAere I get how many words a field have

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


If you are trying to split a space separated string you can use this function:

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

And pass ' ' as your delimiter.

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


In SQL Server 2008 you can use sys.dm_fts_parser to split a string into words. You could incorporate this with 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

Output

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
0

精彩评论

暂无评论...
验证码 换一张
取 消