Solved

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

Posted on 2014-02-18
7
403 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
[X]
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
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 25

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
The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

 
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

Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

Question has a verified solution.

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

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

730 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