Solved

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

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

Expert Comment

by:awking00
ID: 39871151
update piecedetails
set zip9 = replace(zip9,'-','')
where right(zip9,1) = '-';
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
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 31

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

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 …
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
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.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

762 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

19 Experts available now in Live!

Get 1:1 Help Now