[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

combine multiple ntext columns into one pre-existing column

Posted on 2004-09-13
4
Medium Priority
?
623 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 2000 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

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

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

In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
What if you have to shut down the entire Citrix infrastructure for hardware maintenance, software upgrades or "the unknown"? I developed this plan for "the unknown" and hope that it helps you as well. This article explains how to properly shut down …
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

656 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