Solved

conditional SQL query to blank out a field

Posted on 2004-09-23
7
351 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
ID: 12133987
Use the LIKE keyword with the any-set-of-characters wildcard "%":

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

Author Comment

by:SDEWIS
ID: 12134017
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
ID: 12134133
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
ID: 12134157
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

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

Having an SQL database can be a big investment for a small company. Hardware, setup and of course, the price of software all add up to a big bill that some companies may not be able to absorb.  Luckily, there is a free version SQL Express, but does …
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

920 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

17 Experts available now in Live!

Get 1:1 Help Now