Solved

Remove first two characters and place in seperate field

Posted on 2015-01-23
7
96 Views
Last Modified: 2015-01-24
Hi,

Is it possible to remove the first two characters from a table column and place those characters in another separate field.
The fields are both VARCHAR.
Any help would be appreciated.
Thanks
0
Comment
Question by:Morpheus7
7 Comments
 
LVL 24

Expert Comment

by:Phillip Burton
ID: 40566062
Select myField, substring(myField,3,len(myField)-2) as FromCharacter3, substring(myField,1,2) as FirstTwoCharacters
from MyTable
0
 
LVL 49

Expert Comment

by:Vitor Montalvão
ID: 40566066
Sure:
UPDATE TableName
SET ColTwo = LEFT(ColOne,2),
   ColOne = RIGHT(ColOne,LEN(ColOne)-2),

Open in new window

0
 
LVL 49

Accepted Solution

by:
Vitor Montalvão earned 250 total points
ID: 40566070
ColOne is the field to remove the first two characters.
ColTwo is the field where those two characters will be placed.
0
Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

 
LVL 25

Assisted Solution

by:Lee Savidge
Lee Savidge earned 250 total points
ID: 40566072
If the data has leading or trailing spaces, you may want to trim and you can avoid the rows that have no data in the field.

update mytable
set mycol2 = left(ltrim(rtrim(mycol1)), 2),
   mycol1 = right(ltrim(rtrim(mycol1)), len(ltrim(rtrim(mycol1))) - 2)
where ltrim(rtrim(isnull(mycol1, ''))) <> ''

Open in new window

0
 
LVL 69

Expert Comment

by:Scott Pletcher
ID: 40566636
FYI, the separate column with only two characters would be more efficient as:
char(2)
rather than varchar(2).
0
 
LVL 50

Expert Comment

by:Lowfatspread
ID: 40567955
be aware that len will result in you losing trailing spaces from the column if that could be important to you.

you might want to use datalength instead which deals with numbers of bytes (which need to be divided by 2 if it is an NVARCHAR column)
0
 

Author Closing Comment

by:Morpheus7
ID: 40568010
Thanks to all
0

Featured Post

Backup Solution for AWS

Read about how CloudBerry Backup fully integrates your backups with Amazon S3 and Amazon Glacier to provide military-grade encryption and dramatically cut storage costs on any platform.

Question has a verified solution.

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

Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Via a live example, show how to shrink a transaction log file down to a reasonable size.

726 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