SQL Query

Soldato
Joined
28 Sep 2008
Posts
14,158
Location
Britain
Chaps

I have a table with MAC Addresses in, loads in fact.

I need to clear the table completely, but not the columns, just the data in the columns. I can't write 'NULL' to the table as it's protected from that and I don't really want to change it.

Instead, I've used a wildcard query to remove MAC addresses.

Let's say, I have MACs that start with 9C, EC and 08.

Code:
delete from table where macaddress like '9C:%'

will remove any MAC address entry that starts with 9C - fine :)

But, how do I make the query so that I can easily say, delete from table where macaddress like 9C, EC, 08, etc, etc, etc?

Thanks
 
Associate
Joined
25 Jun 2009
Posts
1,260
Location
Guernsey
I don't think you can do anything along the lines of

IN(x, y, z)

with LIKE, so the best that immediately comes to mind is:

DELETE FROM table
WHERE macaddress LIKE '9c:%'
OR macaddress LIKE 'EC:%'
OR macaddress LIKE '08:%'

and so on
 
Soldato
Joined
28 Oct 2006
Posts
12,456
Location
Sufferlandria
Also, if you want to clear the table completely you can just do:

DELETE FROM table;

Yep, doing this wont delete any columns, just the data rows.

DROP table deletes whole table (including columns)
DELETE FROM table deletes data from table but the empty table is still there with all the columns, relationships and other settings.
 
Associate
Joined
15 Jan 2007
Posts
1,071
Also, if you want to clear the table completely you can just do:

DELETE FROM table;

If you're using SQL Server then running the following query is quicker than DELETE FROM to remove all records from a table:
TRUNCATE TABLE <table_name>;
 
Soldato
Joined
18 Oct 2002
Posts
4,898
You might need to specify the table owner or schema depending on the database structure.

I'd use TRUNCATE TABLE owner.tablename

eg. dbo.tablename

The difference between delete from and truncate is that deletes are fully logged in the transaction log and fires any triggers, and truncate doesn't, so it's faster.
 
Associate
Joined
19 Jul 2011
Posts
2,346
Do you want to delete those Rows? Because it sounds like you actually want to blank the MacAddress field. Which is different.

DELETE FROM whateverthetablenameis WHERE macaddress LIKE 'aa%' OR macaddress LIKE 'bb%' or macaddress LIKE 'cc%';

versus

UPDATE whateverthetablenameis SET macaddress=' ' WHERE macaddress LIKE 'aa%' OR macaddress LIKE 'bb%' or macaddress LIKE 'cc%';

:)

Edit: of course, I may just be misreading it...
 
Back
Top Bottom