Solved

MS SQL Update Script

Posted on 2010-08-31
3
290 Views
Last Modified: 2012-05-10
I need to update a Alpha Numeric Syntax to include a couple more Digits.

In RegEx the Syntax is
new Regex("^[A-Z]{2,}\\d{2}-\\d{6}$")

I want to do a SQL statement to update this to

new Regex("^[A-Z]{2,}\\d{2}-\\d{9}$")

A couple examples
AA10-123456
BB01-654321

updated to
AA10-000123456
BB01-000654321

If I can just execute a script it would be ideal, but please let me know what you think.
I also would like to search and replace a Text string that might contain several of these numbers.

If I cannot find a nice script I will make a c# function to do it.






0
Comment
Question by:EazyWorks
  • 2
3 Comments
 
LVL 16

Assisted Solution

by:vdr1620
vdr1620 earned 500 total points
ID: 33570652
You can use a sql below as a smaple to update your column values

UPDATE  TableName
SET ColumnName = SUBSTRING(ColumnName,1,Charindex('-',ColumnName,1)) + '000'+ RTRIM(SUBSTRING(ColumnName,Charindex('-',ColumnName,1),Len(ColumnName)))
WHERE CHARINDEX('-',ColumnName)>0

0
 

Author Comment

by:EazyWorks
ID: 33571208
This will work, I had to change it to
SET ColumnName = SUBSTRING(ColumnName,1,Charindex('-',ColumnName,1)) + '000'+ RTRIM(SUBSTRING(ColumnName,Charindex('-'+1,ColumnName,1),Len(ColumnName)))

Is there any way I can run a Regular Expression for the where and the Update?
javascript:void(toggleRichText('textBottom',1,'/Microsoft/Development/MS-SQL-Server/SQL-Server-2005/Q_26442408.html%3Fcid%3D239%23notices'))
0
 
LVL 16

Accepted Solution

by:
vdr1620 earned 500 total points
ID: 33571267
0

Featured Post

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Data is not showing from images 15 37
Error on Add method 1 38
TSQL DateADD update Question 4 29
C# guarantee sql connection close 6 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 I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
As a trusted technology advisor to your customers you are likely getting the daily question of, ‘should I put this in the cloud?’ As customer demands for cloud services increases, companies will see a shift from traditional buying patterns to new…

920 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

12 Experts available now in Live!

Get 1:1 Help Now