Solved

conditional SQL query to blank out a field

Posted on 2004-09-23
7
350 Views
Last Modified: 2008-02-20
Hi guys

I'm not an SQL query guru by any means, so I'm asking here!

I have an SQL based accounting application.

In my stock records, some of the product records begin with 94xxx.

I field in the same table has been filled in incorrectly for certain 94xxx records, and needs to be blanked out - i.e.be empty.

Trouble is, there's around 4-500 records to go through, something I don't fancy doing by hand!

What would be an SQL query which would search all records beginning with 94 (say, field 1), and delete the contents of field 2 if it contains anything?

thanks in advance

Sean
0
Comment
Question by:SDEWIS
  • 2
7 Comments
 
LVL 15

Expert Comment

by:jdlambert1
Comment Utility
Use the LIKE keyword with the any-set-of-characters wildcard "%":

SELECT *
FROM TableA
WHERE field1 LIKE '94%'
0
 

Author Comment

by:SDEWIS
Comment Utility
thanks jd, that selects the fields, I'm guessing.

Question is, how do I make field 2 in that selection empty/blank?
0
 
LVL 9

Accepted Solution

by:
PaulBarbin earned 125 total points
Comment Utility
update myTable
Set field2 = Null
where field1 like '94%' and field2 is not null

Technically, you don't even need the second condition in the where, but its better not to update all rows when you don't need to.

Of course, as usual, be careful with update queries, they can really wreck havoc, if its not what you want.  Backup or try this on test data first!

Paul

0
 

Author Comment

by:SDEWIS
Comment Utility
That hit the spot more full on!  Thanks Paul - don't worry, I'll be trying it out on my test/wrecker database first!
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Suggested Solutions

Introduced in Microsoft SQL Server 2005, the Copy Database Wizard (http://msdn.microsoft.com/en-us/library/ms188664.aspx) is useful in copying databases and associated objects between SQL instances; therefore, it is a good migration and upgrade tool…
I wrote this interesting script that really help me find jobs or procedures when working in a huge environment. I could I have written it as a Procedure but then I would have to have it on each machine or have a link to a server-related search that …
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.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

743 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

16 Experts available now in Live!

Get 1:1 Help Now