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 2008 Forums
 Transact-SQL (2008)
 Query Help

Author  Topic 

sqlfresher2k7
Aged Yak Warrior

623 Posts

Posted - 2012-02-27 : 10:56:55
[code]

I would like to convert text string(Vcomments) to varchar and insert the records from source table @TableSvar column Vcomments which is text datatype to destination table @TableDvar Vcomments which is varchar(20) and add a new record if the length is greater than varchar(20).

Source table:

declare @TableSvar table (
NewPK int identity(1,1),
CustomerID varchar(5) NOT NULL,
Vcomments text
)

Destination table:

declare @TableDvar table (
NewPK int identity(1,1),
CustomerID varchar(5) NOT NULL,
Vcomments Varchar(20)
)

Insert Into @TablesVar (CustomerID,vcomments)
Select 'A001','The order has been placed for the customer and may recieve in 25 days and deliever at specified address'
union all
Select 'A002','The order has been placed for the customer and may recieve in 35 days and deliever to the specified location.'

Select *
from @TableSVar


Expected output:

NewPK CustomerID Vcomments
----- ----------- --------------------
1 A001 The order has been p
2 A001 laced for the custom
3 A001 er and may recieve i
4 A001 n 25 days and deliev
5 A001 er at specified addr
6 A001 ess
7 A002 The order has been pl
8 A002 aced for the customer
9 A002 and may recieve in 35
10 A002 days and deliever to
11 A002 the specified locatio
12 A002 n.

[/code]

Transact Charlie
Master Smack Fu Yak Hacker

3451 Posts

Posted - 2012-02-27 : 11:02:17
why? I can't see why you would want to.

Maybe you want a VARCHAR(MAX) datatype instead and keep in in one place.....

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

Transact Charlie
Master Smack Fu Yak Hacker

3451 Posts

Posted - 2012-02-27 : 11:11:02
if you must you can use a number table for this. Example

declare @TableSvar table (
NewPK int identity(1,1),
CustomerID varchar(5) NOT NULL,
Vcomments text
)

Insert Into @TablesVar (CustomerID,vcomments)
Select 'A001','The order has been placed for the customer and may recieve in 25 days and deliever at specified address'
union all
Select 'A002','The order has been placed for the customer and may recieve in 35 days and deliever to the specified location.'

DECLARE @widthSplit INT = 20

-- Make a number table. This would be better with a permanent number table....
; WITH numbers ([n]) AS (
SELECT ROW_NUMBER() OVER (ORDER BY a.[a])
FROM
(SELECT 1 AS [a] UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5) AS a
CROSS JOIN (SELECT 1 AS [a] UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5) AS b
)
SELECT
[CustomerID]
, ROW_NUMBER() OVER ( ORDER BY s.[CustomerID], n.[n]) AS [NewPK]
, SUBSTRING(s.[Vcomments], (n.[n]-1) * @widthSplit, @widthSplit) AS [VComments]
FROM
@TableSvar AS s
CROSS JOIN numbers AS n
WHERE
n.[n] <= (LEN(CAST(s.[Vcomments] AS VARCHAR(MAX))) / @widthSplit) + 1
ORDER BY
[CustomerID]
, n.[n]


Results:

CustomerID NewPK VComments
---------- -------------------- -----------------------
A001 1 The order has been
A001 2 placed for the custo
A001 3 mer and may recieve
A001 4 in 25 days and delie
A001 5 ver at specified add
A001 6 ress
A002 7 The order has been
A002 8 placed for the custo
A002 9 mer and may recieve
A002 10 in 35 days and delie
A002 11 ver to the specified
A002 12 location.


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

sqlfresher2k7
Aged Yak Warrior

623 Posts

Posted - 2012-02-27 : 11:13:19
Thanks Charlie.

The destination table has varchar(20) which cannot be modified at this point of time due to limatation.

I need a query which would insert multiple records based on the text value from source table.
Go to Top of Page

Transact Charlie
Master Smack Fu Yak Hacker

3451 Posts

Posted - 2012-02-27 : 11:15:29
try the sql I posted. Any questions just ask.

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

sqlfresher2k7
Aged Yak Warrior

623 Posts

Posted - 2012-02-27 : 11:16:38
Thanks Charlie.

It works..
Go to Top of Page

Transact Charlie
Master Smack Fu Yak Hacker

3451 Posts

Posted - 2012-02-27 : 11:18:12
you should make a permanent number table with a primary key. Number tables are amazing when you work out how to use them.

Read here for more info:
http://www.sqlservercentral.com/articles/T-SQL/62867/

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

- Advertisement -