SQL - replace all column values with 'X' the same length - sql

I am trying to create a stored procedure that replaces all values in one column with Xs the same length as the original values. Here is what I have so far:
SELECT REPLICATE('x', LEN(Name))
This code shows the output with Xs but it does not make this change permanently in the database. Is there a way to make this change permanently in the database?

You need to use an UPDATE statement to physically modify the record:
Update YourTable
Set Name = Replicate('x', Len(Name))
However, I would caution that this will update EVERY record in your table to just XXXX.... You will be effectively removing/destroying all data in that column.
Please do not run this statement unless you are absolutely certain this is what you intend to do.
If your goal actually is to remove all data from that column for every record, and your field is nullable, you could save some space by doing:
Update YourTable
Set Name = Null

Related

How to populate dummy data into an existing table?

I have a large table with given number of rows in which I'd like to replace personal informations with dummy data. I've written functions for this but actually struggling with how to implement it.
I'd like to do something like:
ALTER TABLE SomeTable DROP COLUMN SomeName
ALTER TABLE SomeTable ADD COLUMN SomeName NVARCHAR(30) DEFAULT (SELECT * FROM dbo.FakeName)
Help would be appreciated.
Instead of dropping and adding a column, just do an UPDATE.
If you just want to update the actual data with dummy data , why can't you use update statement as below. We do almost similar in our day to day work. For ex. if we would like to sanitize actual email address of users while restoring the data in my local or test machine (in column SomeName) and in another column we just want to update it with 'XXX' .
UPDATE SomeTable
SET Email_address= SUBSTRING(Email_address,0,CHARINDEX('#',Email_address)) + '#mytest.com',
SomeName2= 'XXX',

Get values based on newly inserted value using SQL

I want to make filtration on a column after selecting a specific value of another column in the same table, I tried to use #... special character followed by the column's name to get the address of this value.
My SQL statement is like the following :
SELECT ATTRIBUTE FROM TABLE WHERE FIELD = '#FIELDNAME';
If I used a specific value instead of #FIELDNAME, it will work properly but it will be static but I need it to be dynamic based on the selected value.
Create another table which will have the list of values that are in the FIELDNAME and give each record a unique id ,then retrieve the value depending on what you have selected by the name of the new table's field preceded by '#...'
I don't know if that what are you looking for, please let me know.
If no triggers are allowed, do you have any date/time column in the table? Is it possible to have that extra column anyway to see the time of a newly inserted row?
You may have to check the lastest row entered, save its field value into a variable. Then do the select based on the variable value.
Based on the vague last row id you could try the following (it's not pretty). But again, if you have date/time that's more accurate.
select attribute from table
where field = (select field from table
where rowid =(select max(rowid) from table))
;
upate
Do you have the priviledge to set up your insert command as below:
insert into table (id, col1, col2,...) values (1,'something', 'something',...)
returning id into variable; -- you may either save field or id depending on your table
Then you may use this variable to select the records you want.

SQL Update or Replace function?

I have a table which contains multiple columns.
Column 1 Column 2 Column 3
unique identifier alphanumerical value numerical value
The unique identifier is currently using the values from Column 2. If I wanted to use the values from Column 3 instead, which would be better suited for my situation? Replace or Update? Or is there another way I should go about doing this.
I'm using TOAD for Oracle for what it is worth.
Thank you.
Turns out what the OP wanted to do was simply set column1 to the value in column3, no replace was necessary. Just a straight update, as in:
UPDATE TheTable SET column1 = column3;
IF
you want to make a change in the table THEN
use UPDATE
ELSE IF
you want to just view the column1 values mapped with column3 values for particular instance THEN
use INSERT
The otherway around use UPDATE to change the contents of table, whereas replace is a function which really doesnt makes any changes in the contents of the table but just shows u the changed output.
You would use both.
update sometable set column1 = replace(column1,column2,column3)
You might want to do the following first to make sure you're replacing what you want to replace:
select replace(column1,column2,column3) from sometable

sql - retain calculated result in calculated field

certain fields in our database contain calculated functions e.g.
select lastname + ', ' + firstname as fullname from contact where contact.id =$contact$
when viewing the field the correct data is shown (i assume this is because when you open the record, the calculation is executed). however, the data is not 'stored' to the field, and therefore is null until the record is opened. is it possible to 'store' the result to the field, making it possible to search the data?
many thanks
james
EDIT
it is not possible for me to create computed_columns using our software.
the above field is a text feild where either 1) a user can manual type in the required data or 2) the database can generate the answer for you (but only whilst you are looking at the record). i know that if I run the following:
Select * from contact where contact.id =$contact$ for xml auto
i only get lastname, firstname - so i know that the fullname field does not retain its information.
If you are using computed columns in sql server, the column is already searchable regardless of whether the calculation result is stored or not. However, if you would like to make it so that the calculation is not run each time you read the row, you can change that under row properties in your Modify Table GUI.
Use the PERSISTED key word when you create the column
From BOL:
PERSISTED
Specifies that the SQL Server Database Engine will physically store the computed values in the table, and update the values when any other columns on which the computed column depends are updated. Marking a computed column as PERSISTED lets you create an index on a computed column that is deterministic, but not precise. For more information, see Creating Indexes on Computed Columns. Any computed columns that are used as partitioning columns of a partitioned table must be explicitly marked PERSISTED. computed_column_expression must be deterministic when PERSISTED is specified.
This isn't the way computed columns work in SQL Server, so I suspect this is something your client application is doing. How are you looking at the data when the value is computed correctly? Does it work when you view the data in SSMS?
Take a look at http://msdn.microsoft.com/en-us/library/ms191250(v=SQL.90).aspx to see how to create computed columns properly.
eg.
create table TestTable
(a int,
b int,
c as a + b)
insert into TestTable (a,b)
values (1,2)
select * from TestTable
where c = 3
This query is based on the computed column and it returns the row that's been inserted.
You need to use the PERSISTED option on a column when you use CREATE TABLE e.g.
CREATE TABLE test (col_a INT, col_b INT, col_c AS col_A * col_B PERSISTED)

How to merge existing row with new data in SQLite?

I have a database full of simple note data, with columns for title, due date, priority, and details. There is also a _id column PRIMARY KEY int.
Say I have a note in the table already with some data filled and the rest null. I also have a set of data that will fill all those fields.
Is there a way that I can only write data to the fields that are NULL?
I can't overwrite existing data, but I'd like to add data to NULL columns.
I know the rowId of the target row.
If my target row had rowId of 5, I could do something like this:
UPDATE SET duedate='some date', priority='2', details='some text' WHERE _id=5
But that would overwrite all the data in that row, and I don't want to lose any data that might be there. How can I change this statement to avoid writing to non-null fields?
Suppose you start with
CREATE TABLE "t" ("a" , "b" , "c" );
INSERT INTO "t" ("a", "c") VALUES (1, 3);
Then
update t set a = coalesce(a,9), b = coalesce(b,10), c = coalesce(c,11);
Will update only the null values, ie. only column B will be set to 10. A and C will be left alone because they contain values.
Coalesce means pick the first item in the list that is not null.
The UPDATE statement only changes the fields you specify in the SET clause. If there are fields whose value you want left unmodified, then simply don't specify those fields in the SET clause.
Put another way, UPDATE doesn't write to all fields in the table - just the fields you specify for the rows you select with the WHERE clause.
If you simply don't know if the existing data is NULL or not, you can set the values using IFNULL(CurrentValue, NewValueIfNull). E.g.
UPDATE SET due_date=IFNULL(due_date, "some date") ... etc..
This will merge your new values into the row where there NULL values, and leave non-NULL values as they were.
See SQL Lite, IFNULL
How about:
UPDATE SET duedate='some date' WHERE _id=5 and duedate is null;
UPDATE SET priority='2' WHERE _id=5 and priority is null;
UPDATE SET details='some text' WHERE _id=5 and priority is null;
If you use Mysql, you can lookup IF()-then you can create a one liner. I think you can do something with similar in Oracle with case...