find-replace across a whole table in SQL - sql

I have multiple SQL tables , each with several columns where, due to bad php code, values that should be NULL are 0.
I want to change all the 0s to NULLs in all columns and all rows.
Is there any way that I can quickly find all values that are 0 (irrespective of the column or row they are in) in a table and update them to NULL?
I want to do it with SQL code in phpMyAdmin.

I cant think of any easier way to do this
update yourtable
Set col1 = case when col1 = 0 then NULL else col1 end,
col2 = case when col2 = 0 then NULL else col2 end,
....
Where 0 in(col1,col2,..)
You have to do this to all your tables. If Mysql supports dynamic Sql then you can use it to frame the update queries with the help of INFORMATION_SCHEMA COLUMNS Table

Related

(Oracle SQL, PLSQL) How to delete columns, which are entirely NULL

I have a question regarding my Oracle SQL / PLSQL Table:
I have a Table with Values. Some of the Columns have no Values in all Rows at all. How do I therefore delete these columns, which are entirely NULL?
incase someones suggestion includes to transpose the tablem its too difficult for me to use Pivot, because the values are too different.
The Table has about 40 columns and 121 Rows.
Thank you very much.
EDIT: what i meant was, it would be great if the empty columns could be "automatically" be detected and deleted/removed/deselected.
You can identify the columns using aggregation. Here is one method that puts the names in a single column:
select ((case when count(col1) = 0 then 'col1,' end) ||
(case when count(col2) = 0 then 'col2,' end) ||
. . .
) as cols
from t;
Then you can use alter table to actually drop the columns.

How to update certain columns in table to truncate records after certain length and regex replace string?

I have a list of columns in a table that I want to truncate to 255 records max and remove any percent signs from the columns, how would I do this?
old Table
col1 col2
adfaadfadfadfdfdafdjf;kldjf;adjsfjads;f 60%
new Table
col1 col2
adfaadfadfadf 60
col1 is not representative of the full string only used for illustration purposes.
I am using sql server 2012.
code so far:
SELECT
case
when len(col)=255
then left(col, 255)
else col end col
from table
Is this not as simple as...?
UPDATE YourTable
SET StringColumn = LEFT(StringColumn,255),
PercentColumn = REPLACE(PercentColumn,'%','')
GO
--You then probably want to fix that column's datatype.
ALTER TABLE YourTable ALTER COLUMN PercentColumn int; --Assuming integer values only.

SQL Server insert into next column

SQL Server 2012: is it possible to do a insert into select statement but insert data into the next column if the previous is already populated?
I have a table with a number of columns to store dates, I want to take data from another table and insert into the next available null date column.
If you are saying that the previous column is already populated, then you're really looking for some sort of update. Try this:
UPDATE yourTable
SET
col1 = CASE WHEN col1 IS NULL THEN 'value' ELSE col1 END,
col2 = CASE WHEN col1 IS NOT NULL THEN 'value' ELSE col2 END;
The logic here is what you described, namely that we attempt to update col1 with some value. Should that column be empty, we make the update, otherwise we update col2 instead.

Add columns dynamically based on a condition in SQL

I'm using SSMS 2012. Consider a table A with columns A, B, C. I would like to dynamically add a new column, D to this table based on a condition in one of the existing columns. How can this be achieved?
Query looks like the following,
select col1, col2 from #temptbl
To this #temptbl, I would like to add a column, col3 and the value in col3 is based on a condition on col2.
To add a column, you need to ALTER the table
Let say you want to add an integer for COL3
ALTER TABLE #temptbl
ADD col3 INTEGER
And to update the value in col3 based on a condition on col2, you need to do an UPDATE and use a CASE statement to check the condition :
Let say you want to set value 3 in COL3 if COL2 is equal to 2
UPDATE #temptbl
SET col3 = CASE WHEN col2 = 2 THEN 3 ELSE null END
After your edit it is still incredibly vague what you want to do but maybe this is close??? No idea what the condition or the output should be but when the condition is not met the output of this will be NULL.
select col1
, col2
, case when SomeCondition then SomeValue end as Col3
from #temptbl
Like Tom mentioned, you can only had a column for the whole table, not on a conditional basis. You can make it a computed column or when you query the table, have a CASE statement if adding a column is not possible.

Delete all data in a table but in one column in access 2007 using Sql query

I'm using access 2007, I have almost 40 columns in a table and i want to reset all data to null except for one column. I am looking for the proper query like this
Delete table.* from table except one column
Anyone knows how to do this?
You could use UPDATE.
UPDATE table
SET
col1 = null,
col2 = null,
col4 = null,
....
WHERE 1=1
This will leave all the data in col3 intact.