How to truncate table with SQL Server 2008? - sql-server-2008

I want to truncate table but when column value equals NULL
truncate table FB_Player where FB_Player.Status ='NULL'

You can not Use Where Clause with truncate. Truncate clears all the data from a table without any condition. Instead you use Delete command.

You are looking for Delete not Truncate.
You cannot use where clause with TRUNCATE.
You can try this:-
delete from FB_Player where FB_Player.Status is NULL
From wiki
You cannot specify a WHERE clause in a TRUNCATE TABLE statement—it is
all or nothing.

Related

How to use IF EXISTS to check whether table exists before removing data in that table

I wanted to check whether a table exists before deleting the values inside the table. In SQL Server we can do as simple as so :
IF EXISTS (SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'table_to_be_edited')
BEGIN
DELETE FROM table_to_be_edited;
END;
but how do we do it in MySQL ?
I am using MySQL Workbench V8.0.
When delete an option is to ignore the table not found error. This eliminates race conditions where a table is created between the test and the truncate. Always consider this when doing SQL operations.

How to change the type of a column?

I have a table in SQL SERVER that has several columns. One of this column had type: VARCHAR(1000). But I want to change it to VARCHAR(MAX).
How to I do this with execute a query?
You can use something like:
ALTER TABLE [table] ALTER COLUMN [column] VARCHAR(MAX)
Do Something like This
ALTER TABLE [table] ALTER COLUMN [column] VARCHAR(MAX)
But I prefer using fixed size in a column Instead of MAX Because It takes more memory and Hence somehow reduces the speed.
try this:
You just need to do the ALTER TABLE statement in sql server
alter table <table> alter column col_name varchar(max)
Optionally you could give NULL/NOT NULL, DEFAULT etc.. along with the alter statement itself
See this link for more examples

MySQL - Select and Update in a single command

I need to select a transactionID from a MySQL table and immediately increment it.
SELECT transid FROM idtable;
UPDATE idtable SET transid=transid +1;
I would like to combine the queries but cannot get the correct syntax.
Using a WHERE clause in your UPDATE will have the same effect.
UPDATE table
SET column = column + 1
WHERE column = value
You could use a sub-query style approach, but I have to wonder if there's any need for the initial SELECT. (Can't you just use a WHERE clause on the UPDATE, perhaps involving a multiple table join if so required.)
Take a look at the MySQL UPDATE query syntax for more information.

ALTER TABLE ORDER BY str_to_date and time_to_sec

I am trying to re-order a table after importing a file but am getting a mysql syntax error on this query:
ALTER TABLE tablename ORDER BY str_to_date(date, '%m/%d/%Y'), time_to_sec(time) ASC
Can anyone see anything wrong with this? this is the error I am getting:
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '(date, '%m/%d/%Y'), time_to_sec(time) ASC' at line 1
According to mysql manual, ALTER TABLE ORDER BY expects a col_name, but you are trying to order it using a function.
I suggest create 2 new columns, populate them with function results, and then order by them.
You might have to use a temp table for this since you're ordering by a function.
CREATE TABLE temp_tablename AS SELECT * FROM tablename;
TRUNCATE tablename;
INSERT INTO tablename SELECT * FROM temp_tablename;
DROP temp_tablename;
You could make the first statement a CREATE TEMPORARY TABLE, but if you lose your session you lose your data.
Did you try
ALTER TABLE tablename ORDER BY date, time ASC
I mention this because it might give you the order you need.
Otherwise you'll need to either do as German Rumm suggested, add columns with the correct datatype, or do your ordering when you do your SQL.
I think I resolved this issue. I was storing imported data in a temp table before moving it onto it's permanent table and needed to sort the data in the temp table first before inserting into the new table. so what I do instead is insert into the new table via a select statement which has the order by statement.
You are using an older version of mysql, STR_TO_DATE is only availabe from version 4.1.1 of MySQL.
Update MySQL version.

How can I remove every column in a table in MySQL?

In MySQL, is there a way to drop every field in my table, short of using:
ALTER TABLE `table`
DROP COLUMN d1,
DROP COLUMN d1,
etc....
Almost like TRUNCATE for fields maybe?
You'll get an error when you try to drop the last column:
ERROR 1090 (42000): You can't delete all columns with ALTER TABLE; use DROP TABLE instead
Says it all! There's no way to have a table with zero columns.
DROP TABLE table