?
Solved

SQL Server: Split a nvarchar(10) column with data into two nvarchar(5) columns

Posted on 2007-09-28
3
Medium Priority
?
892 Views
Last Modified: 2008-03-03
I imported a data set into SQL and it looks like I have incorrectly mapped a column, setting two nvarchar(5) columns as one nvarchar(10) column. This column already has data, how do I safely split the column in two?
0
Comment
Question by:coldchillin
3 Comments
 
LVL 75

Expert Comment

by:Aneesh Retnakaran
ID: 19979556
Any criteria to split the data ?
0
 
LVL 1

Author Comment

by:coldchillin
ID: 19979630
In half. The data will look like this

abcdeabcde
abcd abcd
abc   abcde
abcde
0
 
LVL 15

Accepted Solution

by:
dbbishop earned 2000 total points
ID: 19979905
BEGIN TRANSACTION
UPDATE myTable
SET col2 = SUBSTRING(col, 6, 5)

UPDATE myTable
SET col = SUBSTRING(col, 1, 5)

SELECT * FROM myTable

If everything looks okay then:
COMMIT TRANSACTION
else ROLLBACK TRANSACTION
0

Featured Post

Fill in the form and get your FREE NFR key NOW!

Veeam is happy to provide a FREE NFR server license to certified engineers, trainers, and bloggers.  It allows for the non‑production use of Veeam Agent for Microsoft Windows. This license is valid for five workstations and two servers.

Question has a verified solution.

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

An alternative to the "For XML" way of pivoting and concatenating result sets into strings, and an easy introduction to "common table expressions" (CTEs). Being someone who is always looking for alternatives to "work your data", I came across this …
One of the most important things in an application is the query performance. This article intends to give you good tips to improve the performance of your queries.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
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.
Suggested Courses

850 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