Solved

Remove first two characters and place in seperate field

Posted on 2015-01-23
7
95 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 48

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 48

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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
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

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

837 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