Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 merge text columns

Author  Topic 

marge
Starting Member

2 Posts

Posted - 2010-09-30 : 10:44:50
Newbie, here. In our database there are two ntext cols that I want to merge into one column. Any thoughts on how to do that in T-sql? (I need to concat the entries of one col with those of another.) Also, some of the entries are well over 4000 chars. I understand that we're supposed to stop using ntext and go to nvarchar max. Can that type handle the larger entries?

marge

Transact Charlie
Master Smack Fu Yak Hacker

3451 Posts

Posted - 2010-09-30 : 10:55:10
VARCHAR(MAX) and NVARCHAR(MAX) can handle 2^31-1 bytes of data (which should be enough for most cases).

You'll want to avoid an implicit conversion so you could do something like this

DROP TABLE _TEST
CREATE TABLE _TEST (
[foo1] NTEXT
, [foo2] NTEXT
)

INSERT _TEST ([foo1], [foo2])
SELECT
REPLICATE('foooo1', 1000)
, REPLICATE('fooooo2', 1000)


SELECT
DATALENGTH([foo1]) AS [Bytes Foo1]
, DATALENGTH([foo2]) AS [Bytes Foo2]
FROM
_TEST

ALTER TABLE _TEST ADD [foo3] NVARCHAR(MAX)


SELECT
DATALENGTH([foo1]) AS [Bytes Foo1]
, DATALENGTH([foo2]) AS [Bytes Foo2]
, DATALENGTH([foo3]) AS [Bytes Foo3]
FROM
_TEST

UPDATE _Test SET
[foo3] = CAST([foo1] AS NVARCHAR(MAX)) + CAST([foo2] AS NVARCHAR(MAX))

SELECT
DATALENGTH([foo1]) AS [Bytes Foo1]
, DATALENGTH([foo2]) AS [Bytes Foo2]
, DATALENGTH([foo3]) AS [Bytes Foo3]
FROM
_TEST


Charlie
===============================================================
Msg 3903, Level 16, State 1, Line 1736
The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION
Go to Top of Page

marge
Starting Member

2 Posts

Posted - 2010-09-30 : 15:08:32
Thanks, Charlie. That works for me.

marge
Go to Top of Page
   

- Advertisement -