Click here to Skip to main content
15,885,768 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Dear All,

I have a table named "customer" which has a column "customer_name".

I want to get the particular word from the customer_name column and put it into
another table, say "word_count" which will be having two columns "word" and "count".

Say for example, The customer name in column "customer_name" is "John David Peterson" in "customer" table.

Here, I want to get the three words separately, i.e., "John", "David" and "Peterson" and put them in a table called "word_count".

The "word_count" table should have those 3 words as follows:
Word count
John 1
David 1
Peterson 1

Can any one please give me an idea to write the query or stored procedure for this?

Regards,
Posted

I guess this[^] should be useful to you.
 
Share this answer
 
Comments
Espen Harlinn 27-Jun-11 13:49pm    
Good site, my t
Abhinav S 27-Jun-11 13:52pm    
Thank you Espen.
Raj.rcr 27-Jun-11 15:45pm    
Hiii Abhinav.. The link you have furnished is really good. But, I am not getting, how to send the names of customer table as parameters to the function which I use in the link provided by you.. Do u have any idea?
It is possible to split words or sentences in SQL, though this is not a built-in function. However, searching the net for some functions returns numerous results.
SQL Split Function 1[^]
SQL Split Function 2[^]
SQL Split Function 3[^]
Etc...

I haven't used or tested those, but they generally work something like:
SQL
--The function returns a table, so you can select from it.
select * from dbo.SplitFunction("This is a test String", ' ')

/*
Return something like:
This
is
a
test
String
*/
 
Share this answer
 
Here's a string splitting function for sql I found that returns a table.

C#
CREATE FUNCTION [dbo].[SDF_SplitString] 
(
    @sString nvarchar(2048),
    @cDelimiter nchar(1)
)
RETURNS @tParts TABLE ( part nvarchar(2048) ) 
AS BEGIN     
    if @sString is null return     
    declare @iStart int, @iPos int     
    if substring( @sString, 1, 1 ) = @cDelimiter     
    begin         
        set @iStart = 2
        insert into @tParts values( null )
    end
    else
        set @iStart = 1
    while 1=1
    begin
        set     @iPos = charindex( @cDelimiter, @sString, @iStart )         
        if @iPos = 0                 
            set     @iPos = len( @sString )+1         
        if @iPos - @iStart > 0                                   
            insert into @tParts values  ( substring( @sString, @iStart, @iPos-@iStart ))         
        else                 
            insert into @tParts values( null )
        set @iStart = @iPos+1
        if @iStart > len( @sString )
            break
    end
    RETURN  
END 
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900