Solved

Remove first two characters and place in seperate field

Posted on 2015-01-23
7
92 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
Comment Utility
Select myField, substring(myField,3,len(myField)-2) as FromCharacter3, substring(myField,1,2) as FirstTwoCharacters
from MyTable
0
 
LVL 45

Expert Comment

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

Open in new window

0
 
LVL 45

Accepted Solution

by:
Vitor Montalvão earned 250 total points
Comment Utility
ColOne is the field to remove the first two characters.
ColTwo is the field where those two characters will be placed.
0
Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

 
LVL 25

Assisted Solution

by:Lee Savidge
Lee Savidge earned 250 total points
Comment Utility
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:ScottPletcher
Comment Utility
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
Comment Utility
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
Comment Utility
Thanks to all
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
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…
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 set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

744 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now