Solved

combine multiple ntext columns into one pre-existing column

Posted on 2004-09-13
4
611 Views
Last Modified: 2010-05-18
Using an SQL query I need to combine several ntext columns into one preexsisting column.  Some columns may be NULL.

Table [Community Profile]

Columns [Community Sales Points],[General Notes],[Public Private Elem Secondary],[Colleges Universities],[Outdoor Activities],[Cultural Activities],[Shopping],[Industry],[Restaurant],[Church Denominations],[Health Fitness Ctrs]

I want to combine all columns into the first column [Community Sales Points].  I know this requires using the UPDATETEXT command, but I'm rather confused on the syntax.
0
Comment
Question by:NoelKent
4 Comments
 
LVL 18

Expert Comment

by:SjoerdVerweij
ID: 12047593
How big is the content of these columns?

0
 
LVL 13

Accepted Solution

by:
ispaleny earned 500 total points
ID: 12049159
The following code provides sample ntext columns transformation A=A+B, modify it to sequential steps
1. A=A+B
2. A=A+C
3. A=A+D
And so on.
Too much code to be generated, but I think it is easy now :)


drop table [Community Profile Test]
create Table [Community Profile Test]
(
 [id] integer primary key
,[Community Sales Points] ntext
,[General Notes] ntext
)
GO
insert [Community Profile Test]([id],[Community Sales Points],[General Notes]) values (1,'2','3')
insert [Community Profile Test]([id],[Community Sales Points],[General Notes]) values (2,NULL,'3')
insert [Community Profile Test]([id],[Community Sales Points],[General Notes]) values (3,NULL,NULL)
insert [Community Profile Test]([id],[Community Sales Points],[General Notes]) values (4,'2',NULL)

select * from [Community Profile Test]

declare @c cursor;
declare @cid integer;
declare @cptr0 varbinary(16);
declare @clen0 integer;
declare @cptr1 varbinary(16);
set @c = cursor for
select
 [id]
,TEXTPTR            ([Community Profile Test].[Community Sales Points])    C0_PTR
,COALESCE(DATALENGTH([Community Profile Test].[Community Sales Points])/2,0) C0_LEN
,TEXTPTR            ([Community Profile Test].[General Notes])    C1_PTR
from [Community Profile Test]
;
open @c;
fetch @c into @cid,@cptr0,@clen0,@cptr1;
while @@fetch_status = 0 begin
 if @cptr1 is not null begin
  if @cptr0 is null begin
   update [Community Profile Test] set [Community Sales Points]='' where [id]=@cid;
   set @cptr0=(select TEXTPTR([Community Profile Test].[Community Sales Points]) from [Community Profile Test] where [id]=@cid);
   set @clen0=0
  end
  UPDATETEXT [Community Profile Test].[Community Sales Points] @cptr0 @clen0 0 [Community Profile Test].[General Notes] @cptr1
 end
 
 fetch @c into @cid,@cptr0,@clen0,@cptr1;
end;
close @c;
deallocate @c;

select * from [Community Profile Test]
0
 

Author Comment

by:NoelKent
ID: 12049400
thanks ispaleny you rock!
0
 

Expert Comment

by:DougR_MS
ID: 12383438
This is a solution I too have been looking for, but what about if the NTEXT field is part of replication?  Where would BEGIN TRAN and COMMIT TRAN belong so that the pointer is all in 1 transaction?

Thanks,
Doug
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now