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
132 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

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Need a complete list of ASP.NET page load events 3 26
How to use odbc in vb to connect to ms sql 14 34
Max Consumption Rate (MCR) 3 32
SQL Server Question 5 25
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.
CodeTwo Sync for iCloud (http://www.codetwo.com/sync-for-icloud?sts=6554) automatically synchronizes your Outlook 2016, 2013, 2010 or 2007 folders with iCloud folders available via iCloud Control Panel. This lets you automatically sync them with…
A simple description of email encryption using a secure portal service. This is one of the choices offered by The Email Laundry for email encryption. The other choices are pdf encryption which creates an encrypted pdf of your email and any attachmen…

911 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

21 Experts available now in Live!

Get 1:1 Help Now