Parse a varchar2 Column Value - oracle

I want to remove a piece of a string from a particular table's column. The string I wish to remove is the &expires and everything after it but leave everything before the &expires the same. Is there a way to accomplish with the update statement or is a stored procedure needed?
Table column value is:
Starting Value: DAABq3J65GvwBABbWdkFOnpCj2mEA1lMonZBZADcTYJR6QuLPUlfZBtMyoEl4x2JXQ49cOzjZAStQxWNOgrurtnMNIw04bmOcQ4SsrjuPKH4AZBBBAf8ZBjWhs8BM52aC0OpnPGzjm6V2x50qk6wboT&expires=5183999
Desired Ending Value:
DAABq3J65GvwBABbWdkFOnpCj2mEA1lMonZBZADcTYJR6QuLPUlfZBtMyoEl4x2JXQ49cOzjZAStQxWNOgrurtnMNIw04bmOcQ4SsrjuPKH4AZBBBAf8ZBjWhs8BM52aC0OpnPGzjm6V2x50qk6wboT

update table set column = regexp_replace(column, '&'||'expires=.*$')

Related

Cascading LOV - Invalid number while updating record - Oracle Apex

I have a select list based on other select list. It works fine on the create screen but gives 'Invalid number' error in update screen.
SQL query:
select description, account_id
from t_account
where chart_id = :P15_chart_name
Please help me out!
You are trying to convert a string into a number, and that failed.
As Select List item has 2 values: 1st being display and the 2nd return value, it is the 2nd value that causes problems - account_id in your case.
For example, it contains value A while column that is supposed to accept this value is declared as NUMBER. Oracle can't convert A into a number and raises an error.
What to do?
make sure that Select List item LoV query returns only numbers, or
modify column's datatype to varchar2
If that's not it, then :P15_CHART_NAME item and chart_id table column differ in datatypes; a simple option is to fix page item's datatype.

Replace data of one column with substring of another column in sql loader

I am loading data from a csv file into a table using sqlldr. There is one column which is not present in every row of the csv file. The data needed to populate this column is present in one of the other columns of the row. I need to split (split(.) )that column's data and populate into that column.
Like:-
column1:- abc.xyz.n
So the unknown column(column2) should be
column2:- xyz
Also, there is another column which is present in the row but it's not what I want to input into the table. It is also needed to be populated from column1. But there are around 50 if-else cases in that. Is decode preferable to do this?
column1:- abc.xyz.n
Then,
column2:- hi if(column1 has 'abc')
if(column1 has 'abd' then 'hello')
like this there are around 50 if-else cases.
Thanks for help.
For the first part of your question, define the column1 data in the control file as BOUNDFILLER with a name that does not match a table column name which tells sqlldr to remember it but don't use it. If you need to load it into a column, use the column name plus the remembered name. For column2, use the remembered BOUNDFILLER name in an expression where it returns the part you need (in this case the 2nd field, allowing for NULLs):
x boundfiller,
column1 EXPRESSION ":x",
column2 EXPRESSION "REGEXP_SUBSTR(:x, '(.*?)(\\.|$)', 1, 2, NULL, 1)"
Note the double backslash is needed else it gets removed as it gets passed to the regex engine from sqlldr and the regex pattern is altered incorrectly. A quirk I guess.
Anyway after this column1 ends up with "abc.xyz.n" and column2 gets "xyz".
For the second part of your question, you could use an expression as already shown but call a custom function you create where you pass the extracted value and it would return the searched value from a lookup table. You certainly don't want to hardcode your 50 lookup values. You could do the same thing basically in a table level trigger too. Note I show a select statement for an example only but this should be encapsulated in a function for reusability and maintainability:
Just to show you can do it:
col2 EXPRESSION "(select 'hello' from dual where REGEXP_SUBSTR(:x, '(.*?)(\\.|$)', 1, 2, NULL, 1) = 'xyz')"
The right way:
col2 EXPRESSION "(myschema.mylookupfunc(REGEXP_SUBSTR(:x, '(.*?)(\\.|$)', 1, 2, NULL, 1)))"
mylookupfunc returns the result of looking up 'xyz' in the lookup table, i.e. 'hello' as per your example.

Cannot update a row with a bind variable in UPDATE statement

I am using Oracle SQL Developer 4.0.0.13.
Query :
UPDATE employes
SET emptime = systimestamp
WHERE emp_id = 123
AND emp_device = :abc;
Field Definition : emp_device char(20 byte)
Value is : 99998000000008880999 (This value is present in the table)
When I run the above query in SQL developer it asks me to give the value for the bind variable, which I paste in the text box and it returns 0 rows updated.
But when I remove the bind variable in the update query and specify the actual value, it updates the column value. Below is the query.
Query:
UPDATE employes
SET emptime = systimestamp
WHERE emp_id = 123
AND emp_device = 99998000000008880999 ;
---(works)
Also, when I add some trailing spaces in the bind variable text box and trim the emp_device column, it updates the column. Below is the query.
Query :
UPDATE employes
SET emptime = systimestamp
WHERE emp_id = 123
AND emp_device = trim(:abc);
-- (works --- :abc value is '99998000000008880999 ')
I do not know what is wrong with it. Can someone please take a look and suggest a solution.
You are using CHAR type for your emp_device datatype. Note that CHAR type always blank pads the resulting string out to a fixed width.read this.
You should use VARCHAR2 as datatype if you are expecting a string or just NUMBER as your example consists purely of numeric values.
in dialog box enter your parameter as '99998000000008880999' use apostrophe chars.

How to migrate column data to new data type

I need to migrate table's column to another data type with some modifications. Given: table 'USER' with column 'ORDER_TIME' (DateTime format). It's required to change column type to NUMBER, convert that time to a minutes (e.g. 8:00 AM = 480 mins.) and to store new value. As i understand, i need to create new column with required NUMBER data type, iterate over all records, do some recalculations, store new value, drop old column and rename new one to an actual name. So, basically, algorithm is:
ALTER TABLE USER ADD ORDER_TIME_MINS NUMBER(4) NULL;
iteration and recalculation
ALTER TABLE USER
DROP COLUMN ORDER_TIME
RENAME COLUMN ORDER_TIME_MINS TO ORDER_TIME;
Can you suggest me some reading on how that iteration may looks like?
You can try like this:
update tablename
set columnName = to_number(to_char(to_date(yourDateColumn,'hh24:mi:ss'),'sssss'))/60
And if your time is in format 08:00 PM then like
update tablename
set columnName =TO_NUMBER(TO_CHAR(TO_DATE('08:00 AM','HH:MI AM'),'SSSSS'))/60
Rest of the steps which you have shown ie, dropping and renamin are fine.

Alter table - change the default value of a column

I have a column with REAL data type, and I'm trying to write a statement to change the default value to 4. However, when I use the select * statement to double check, the column is still empty.
ALTER TABLE recipes
MODIFY NumberOfServings DEFAULT '4';
The DEFAULT value is used at INSERT time. It gives a default value to be inserted when you do not provide a value for the column in the INSERT statement.
You may want to update the table, so that all NULL values are replaced by a given value:
UPDATE recipes SET NumberOfServings=4 WHERE NumberOfServings IS NULL;
You can also specify a value to use when the column is NULL at QUERY time.
This can be done by using the NVL function:
SELECT NVL(NumberOfServings,4) FROM recipes;
http://docs.oracle.com/cd/B19306_01/server.102/b14200/functions105.htm
The default value applies only when you insert new records into the table. It does not update any existing values in the table. Why do you enclose a numeric value in quotes?

Resources