Solved

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

Posted on 2014-02-18
7
400 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

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SQL Query stumper 3 37
create insert script based on records in a table 4 16
How to use Full Text CONTAINS with Case in SQL 6 20
SQL Query Syntax Join 4 34
I'm trying, I really am. But I've seen so many wrong approaches involving date(time) boundaries I despair about my inability to explain it. I've seen quite a few recently that define a non-leap year as 364 days, or 366 days and the list goes on. …
JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
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.
Via a live example, show how to shrink a transaction log file down to a reasonable size.

863 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

26 Experts available now in Live!

Get 1:1 Help Now