Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

conditional SQL query to blank out a field

Posted on 2004-09-23
7
Medium Priority
?
359 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 500 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

I have a large data set and a SSIS package. How can I load this file in multi threading?
This month, Experts Exchange sat down with resident SQL expert, Jim Horn, for an in-depth look into the makings of a successful career in SQL.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.

963 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