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
Solved

MS SQL Query to change a table field value if it has a specific current value

Posted on 2014-02-18
7
402 Views
Last Modified: 2014-02-20
I have a MS SQL 2008 database called ACIMain. In that database I have a table called PieceDetails. In the PieceDetails table is a varchar field called ZIP9. This table has several million records where 30 thousand or so of records have the ZIP9 value that has 9 digits and a dash. The 9 digits vary from record to record but they all have a dash as the 10th character. I need to select the records that have 9 digits and a dash from all of the records in the table and then remove the dash from each of the selected record.

How can I do that with a T-SQL query?


Thanks,

Dave
0
Comment
Question by:dcadler
7 Comments
 
LVL 35

Accepted Solution

by:
David Todd earned 500 total points
ID: 39869243
Hi,

use ACIMain
go

update pd
set ZIP9 = left( pd.ZIP9, 9 )
-- select pd.zip9, left( pd.zip9, 9 )
from dbo.PieceDetails pd
where
    right( pd.zip9, 1 ) = '-'
;

Select from the select on down and run that. If it looks okay, then run the entire script.

HTH
  David
0
 
LVL 24

Expert Comment

by:chaau
ID: 39869528
I think this query will do the trick:
update PieceDetails
set ZIP9 = STUFF( ZIP9, 10, 1, '' )
where substring( ZIP9, 10, 1 ) = '-'

Open in new window

0
 
LVL 32

Expert Comment

by:awking00
ID: 39871151
update piecedetails
set zip9 = replace(zip9,'-','')
where right(zip9,1) = '-';
0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
LVL 35

Expert Comment

by:David Todd
ID: 39872004
@Awking00 - that's almost exactly what I posted several comments above.
0
 

Author Closing Comment

by:dcadler
ID: 39873314
Thanks David. Thad did the trick
0
 
LVL 32

Expert Comment

by:awking00
ID: 39873333
David Todd, I apologize. I missed reading your earlier post.
0
 
LVL 35

Expert Comment

by:David Todd
ID: 39874432
@Awking00 - apology accepted.
0

Featured Post

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

Suggested Solutions

I have a large data set and a SSIS package. How can I load this file in multi threading?
In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

860 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