Is there a way to duplicate a column from a current database table (copy all the column contents from table to a temporary table), Then
Convert the string value in the column and increment it by 1, then
Put all those values in a form of a string back into it's original table?
So pseudocode would look like:
copy column1 from tblReal into tmpcolumn in tblTemp (set tmpcolumn1 as nvarchar(265))
update tblTemp
set tmpcolumn1 = 'TESTDATA' + 1
copy tbmpcolumn1 from tblTemp into column1 in tblReal
So actually you want to change a string column, which holds actually a number, by incrementing its value by 1. Why would you need three steps for that? Just do an update statement on the column immediatly. I don't see why you need intermediate tables.
UPDATE tblReal SET column1 = column1 + 1
Piece of cake. You can use the cast function to transform the varchar to a number and back again in the update statement.
Related
I have an issue later in my process when I want the append tables with a different Datatypes.
I am creating a new table out of an existing table. One column is the Calenderweek(KW) which was originally a STRING. In order to append my tables later on I need the same datatype for the column.
Is there a way to change the datatype for a column while creating the new table?
CREATE TABLE IF NOT EXISTS
MyNewTable
AS(
SELECT
Column_1 AS
Column_1_alias,
**KW_ AS KW,**
FROM
SourceTable);
What this Query does is that it only grabs the value of the column KW that contains a number, then checks if the STRING value contains a character and removes it from the STRING. Finally it CAST to the desired value type of the column, so it ends as an INT.
CREATE TABLE IF NOT EXISTS
dataset.MyNewTable
AS(
SELECT
Column1 AS
Column1_alias,
CAST(REGEXP_REPLACE(KW,'[^0-9^]','') as INT64) as KW_Alias
FROM
`project.dataset.source`
WHERE REGEXP_CONTAINS(KW,'[0-9]')
);
Another possible solution is to use the function REPLACE instead of REGEXP_REPLACE, to replace the string to a number.
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
I have a redshift table which is used for tracking, and as a result its pretty huge. I need to update one column after applying some text operations and extracting a value from another column.
The query that I have managed to write works only for one row.
UPDATE schema.table_name SET data_id = (SELECT split_part(regexp_substr(data_column,'pattern=[^&]*'),'=',2)::BIGINT FROM schema.table_name where id = 1620) WHERE id = 1620;
How do I get it to work for every row in the table.
UPDATE
schema.table_name
SET
data_id = SPLIT_PART(REGEXP_SUBSTR(data_column, 'pattern=[^&]*'),'=',2)::BIGINT;
Just don't put WHERE id = 1620; at end of update query.
Updates are not efficient in Redshift. If you have a huge table and you intend to update every single row, you should instead copy the data (with the updated column) to a new table and then flip them.
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.
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...