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
128 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
1 Comment
 
LVL 69

Accepted Solution

by:
ScottPletcher 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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
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.
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

746 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

17 Experts available now in Live!

Get 1:1 Help Now