Solved

combine multiple ntext columns into one pre-existing column

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Having an SQL database can be a big investment for a small company. Hardware, setup and of course, the price of software all add up to a big bill that some companies may not be able to absorb.  Luckily, there is a free version SQL Express, but does …
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

810 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