Solved

conditional SQL query to blank out a field

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

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SqlDataBase 7 48
export sql results to csv 6 35
SSIS with VPN COnnection 2 76
TSQL query to generate xml 4 33
Nowadays, some of developer are too much worried about data. Who is using data, who is updating it etc. etc. Because, data is more costlier in term of money and information. So security of data is focusing concern in days. Lets' understand the Au…
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
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…

773 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