Solved

How to write a SQL Server 2008 SQL stat't to populate a 3 character Branch field with the COSTCENTER field starting at position 4?

Posted on 2014-10-28
1
137 Views
Last Modified: 2014-10-28
I am using SQL Server 2008.
How would you write a SQL   UPDATE statement to populate the
BRANCH field, a 3 character field. The source would be the 4th position (up to 3 characters in length) of the COSTCENTER field.


for ex:

UPDATE dbo.tblPSEmail
SET BRANCH = SUBSTRING(COSTCENTER,4,LEN(COSTCENTER)-3)
0
Comment
Question by:zimmer9
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 69

Accepted Solution

by:
Scott Pletcher earned 500 total points
ID: 40409276
Honestly, I wouldn't, I'd use a computed column for Branch.

ALTER TABLE dbo.tblPSEmail
ADD BRANCH AS SUBSTRING(COSTCENTER,4,3)

If there might be only 1 or 2 bytes, and you need to zero-fill them, you could do this:

ALTER TABLE dbo.tblPSEmail
ADD BRANCH AS RIGHT('00' + SUBSTRING(COSTCENTER,4,3), 3)
0

Featured Post

Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How to repeat the data 4 30
Data organization issue 7 39
Asp.Net Session Question 2 36
SQL server client app 3 32
SQL Server engine let you use a Windows account or a SQL Server account to connect to a SQL Server instance. This can be configured immediatly during the SQL Server installation or after in the Server Authentication section in the Server properties …
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

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