I have a column with a string of text inside of it that varies in length. I want to return the first 4 characters in the string starting from position 1 and output them into a table only if there is a direct match. So if I want to return just PDFG and not return ADHR or any other combination then the below code works just fine for me.
use DB
select
substring(description, 1, 4) as newcol from table1 where substring(description, 1, 4) like '%ABCD%'
however I would like to persist this calculated column into an existing table, so something like this;
use DB
alter table table1
add newcol as ("and then the rest of the code above")
I am not sure how to reorder my code to fit the new query any help is appreciated.
Here is some sample data:
PDFG_2013 AHSDHDF
ADHR_2310 ADGDGEE
DATW_5142 NFBSAEE
The output from this should be stored in newcol within an existing table called table1. The only value in new column from the sample data should be PDFG
Use a case expression to return the first 4 characters if matching, otherwise null.
Alter table table1
add newcol as case when description like 'ABCD%' then substring(description, 1, 4) end
Or, even simplier
Alter table table1
add newcol as case when description like 'ABCD%' then 'ABCD' end
Related
I am trying to update a existing column value with new value in sql table.
For example, My table has below data
table1:
ID
RequestName
1
Victor-123
2
Hello-123
3
Victor-124
4
Victor-125
5
Hi-123
6
Victor-126
In the above table I want to update Request Name column value wherever we have Victor, I want to replace with Victor-ID. For example
for ID 1 we have RequestName column value is Victor-123. I want to update it with Victor-ID-123 using Sql. I know we can do it with update sql statement, but if we have lot of data how to achieve that or replace 'Victor' with 'Victor-ID'. Since we might have different values in Request Name column. I want to update only column value with Victor to Victor-ID in table
Any help, I appreciate it
Thank you
UPDATE [tablename] SET [RequestName] = REPLACE(RequestName,'Victor','Victor-ID')
Here are two examples:
Example 1:
update table1
set RequestName = REPLACE(RequestName, 'Victor', 'Victor-ID')
where RequestName like 'Victor-%'
Example 2 (this one will let you use this for any name by changing the where clause):
update table1
set RequestName = LEFT(RequestName, CHARINDEX('-', RequestName) - 1) + '-ID-' + RIGHT(RequestName, LEN(RequestName) - CHARINDEX('-', RequestName))
where RequestName like 'Victor-%'
I don't believe example 1 actually requires the where clause though, as it should only affect those with "Victor". However, if you have something like "Victor1-123" that should not be changed, the where clause will prevent that.
Edit: Just occurred to me that example 1 can be adjusted to:
update table1
set RequestName = REPLACE(RequestName, 'Victor-', 'Victor-ID')
This will allow you to eliminate the where clause.
The case I am trying to solve is this: for every row in a table another row from a second table might exist, so I need all data from the row of the first table and the data from the row of the second table if present.
I know I can use data structures as host variables to gather all data from a row in a table. So, my select is this:
select
t1.*
,t2.*
into
:dst1
,:dst2
from table1 t1
left join table2 t2 on t2.key=t1.key
;
where dst1 and dst2 are data structures respectively like table1 and table2 records' format. Pretty simple.
Now, the point is how to catch null result when a row for that key doesn't exist in the second table. In that case I would like to have the corresponding data structure initialized, but coalesce works on one field at a time and I haven't been able to find another solution.
Is there a way to obtain this result?
Any help would be appreciated!
Thanks
One way to deal with this is to use indicator variables. It looks like this:
dcl-ds hs Qualified;
field1 ...
field2 ...
endds;
dcl-s hsind Int(5) Dim(2);
exec sql
select *
into :hs:hsind
from table
fetch first row only;
Note, there is no comma (,) between :hs and :hsind as this is part of the same variable assignment. :hsind is an indicator variable, and in this case is an array of Int(5) with the same number of elements as the host data structure :hs has fields. The indicator variable will contain a 0 if the value in the associated field in :hs is good, or -1 if it is null. So in our example above: If hs.field1 is good, and hs.field2 is null, then hsind(1) = 0, and hsind(1) = -1. Other values mean other things like data mapping error (-2), or string truncation (positive number with original length of string).
So in your example, use something like this:
select
t1.*
,t2.*
into
:dst1:dst1ind
,:dst2:dst2ind
from table1 t1
left join table2 t2 on t2.key=t1.key
;
Where dst1ind is an array if Int(5) with the same number of elements as dst1 has subfields, similarly for dst2ind. Then after your selection, just check dst2ind(1) >= 0, and you have a good select. Notice that you will need to make sure that select into only returns a single row, or you will get errors about that.
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.
I have a temporary table in a PostgreSQL function and I want to insert a new VARCHAR column. It should have a value that depends on another column of the table, named "amount".
When the amount is positive I would like the value of the column row to be credit and when the value is negative the column should be debit.
I have one more request: I want to round the value of amount column in 2 decimal digits
You want ALTER TABLE ... ADD COLUMN followed by an UPDATE.
I initially said ALTER TABLE ... ADD COLUMN ... USING but that was wrong on two counts. ADD COLUMN takes a DEFAULT not USING - and You can't do it in one pass because neither a DEFAULT expression nor a USING expression may not refer to other columns.
So you must do:
ALTER TABLE tablename ADD COLUMN colname varchar;
UPDATE tablename SET colname = ( CASE WHEN othercol < 0 THEN 'Credit' ELSE 'Debit' END );
Think carefully about whether zero should be 'Debit' or 'Credit' and adjust the CASE accordingly.
For rounding, use round(amount,2). There isn't enough detail in your question for me to be sure how; probably by UPDATEing the temp table with UPDATE thetable SET amount = round(amount,2) but without the context it's hard to know if that's right. That statement irreversibly throws information away so it should only be used on a copy of the data.
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...