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
134 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:
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

U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

Question has a verified solution.

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

Hi all, It is important and often overlooked to understand “Database properties”. Often we see questions about "log files" or "where is the database" and one of the easiest ways to get general information about your database is to use “Database p…
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 …
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…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

786 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