Solved

combine multiple ntext columns into one pre-existing column

Posted on 2004-09-13
4
618 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Creating a View from a CTE 15 44
SQLCMD question to have fields created as fixed length 2 35
Migrate SQL 2005 DB to SQL 2016 4 22
Sql server query 8 19
Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.
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…

737 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