Solved

combine multiple ntext columns into one pre-existing column

Posted on 2004-09-13
4
616 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

Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Nowadays, some of developer are too much worried about data. Who is using data, who is updating it etc. etc. Because, data is more costlier in term of money and information. So security of data is focusing concern in days. Lets' understand the Au…
I have a large data set and a SSIS package. How can I load this file in multi threading?
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

839 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