Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2014-02-18
7
Medium Priority
?
408 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 2000 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
 [eBook] Windows Nano Server

Download this FREE eBook and learn all you need to get started with Windows Nano Server, including deployment options, remote management
and troubleshooting tips and tricks

 
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
When trying to connect from SSMS v17.x to a SQL Server Integration Services 2016 instance or previous version, you get the error “Connecting to the Integration Services service on the computer failed with the following error: 'The specified service …
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.

610 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