Click here to Skip to main content
15,895,656 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi All,

In my scenario i have a title column and i want to select each word in title and insert seperately into another table.
For example:
Table name is Book
Id Tilte
1 One day in my life

second table name is Master
id title
1 one
2 day
3 in
4 my
5 life
like these i want to insert into master table.
Posted

1 solution

You will probably want this as a stored procedure with @INSTR as a parameter, but it should work:
SQL
DECLARE @INSTR as VARCHAR(MAX)
SET @INSTR = 'One day in my life '
DECLARE @SEPERATOR as VARCHAR(1)
DECLARE @SP INT
DECLARE @VALUE VARCHAR(1000)
SET @SEPERATOR = ' '
WHILE PATINDEX('%' + @SEPERATOR + '%', @INSTR ) <> 0
BEGIN
   SELECT  @SP = PATINDEX('%' + @SEPERATOR + '%',@INSTR)
   SELECT  @VALUE = LEFT(@INSTR , @SP - 1)
   SELECT  @INSTR = STUFF(@INSTR, 1, @SP, '')
   INSERT INTO MyTable (Title) VALUES (@VALUE)
END
 
Share this answer
 
Comments
josh-jw 9-Jan-13 4:27am    
Thanks,Griff.
Is it possible that my table containing 10 tiltes.within one select i can pass all titles into this approach and do the above operation?
OriginalGriff 9-Jan-13 4:32am    
Sorry? I don't understand what you are asking.
Would you mind trying to ask that in a different way, perhaps with an example?
josh-jw 9-Jan-13 5:45am    
i would like to say another scenario.
is it possible that i call one stored procedure inside a function and after that i call that function in query like
select title,fn(title) from book.?
OriginalGriff 9-Jan-13 5:54am    
Why would you want to do that? Every time you tried to retrieve info on the title "One day in my life" is would add 5 rows to a table, which already should contain them.
I would create an SP which did both inserts: the main title, and each word and use that instead of trying to insert the title.
What are you trying to achieve with this anyway? There may be a better way to do it.
josh-jw 9-Jan-13 5:58am    
i want to achieve the scenario like my book table containing 10 title and i want to insert that title like each word as one record into master table.i want to do
selecting and inserting at a time,like bulk insertion.

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