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
Solved

conditional SQL query to blank out a field

Posted on 2004-09-23
7
354 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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Introduction SQL Server Integration Services can read XML files, that’s known by every BI developer.  (If you didn’t, don’t worry, I’m aiming this article at newcomers as well.) But how far can you go?  When does the XML Source component become …
Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
Viewers will learn how the fundamental information of how to create a table.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

861 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