Oracle add a virtual column which adds years - sql

I'm trying to add a virtual column of date type which adds five years to another date column:
ALTER TABLE AU_Ventes ADD (
DateVente date NOT NULL,
DateFinGarantie date As(ADD_MONTHS(DateVente, 60))
);
But I get the error: "%s: invalid identifier". Perhaps I can't use the ADD_MONTHS function in an ALTER TABLE. What can I do to accomplish what I want to do?
Thanks.

Although you can define a virtual column in a create table statement, where you are obviously creating it at the same time as the column it is based on, you can't define both in the same alter table statement. You have to do it in two steps:
ALTER TABLE AU_Ventes ADD DateVente date NOT NULL;
ALTER TABLE AU_Ventes ADD DateFinGarantie date
As(ADD_MONTHS(DateVente, 60));
Not sure that's an intentional restriction, but it's how it works.
Trivial SQL Fiddle to show this working.

Related

How to add new column using CAST and info from another table in SQL?

I'm trying to understand the relationship between two operations in SQL - ADD COLUMN and CAST().
I tried to create a new column containing the lengths of another column's values, while that other column is inconveniently of type INTEGER:
ALTER TABLE inventory
ADD inventory_id_len AS (CHAR_LENGTH(CAST(inventory_id AS VARCHAR)) FROM rental);
But it returns:
ERROR: syntax error at or near "AS"
LINE 4: ADD inventory_id_len AS (CHAR_LENGTH(CAST(inventory_id AS V...
Thanks.
In Postgres, you need to use the generated always ... stored syntax to add a computed column. For your use case, that would look like:
alter table inventory
add inventory_id_len int
generated always as (char_length(inventory_id::text)) stored
;
A subquery makes no sense in that context; the computed column takes the value of column inventory_id on the very same row.
If you want to add the length of the id as a generated column:
ALTER TABLE inventory
ADD inventory_id_len INT GENERATED ALWAYS AS (LEN(inventory_id::text) STORED;
Because Postgres does not (yet) support virtual generated columns, a view might be more in line with what you want:
create view v_inventory as
select i.*, len(inventory_id::text) as inventory_id_len
from inventory i;

How do I create a new column filled with the results of a query?

I have a column that is VARCHAR2 and the string inlcudes a date and time. I have extracted the date and now wish to populate a new column solely with the date and time.
So far I have:
alter table t
add cb_time
as
select substr(t.notes, 24, INSTR(t.notes, 'for')-1)
from Mytable t
this results in error ORA 2000 - missing ( keyword
An alternative method is to create CB_TIME as a virtual column. To do soan ALTER TABLE similar to the following is used:
ALTER TABLE T
ADD CB_TIME DATE GENERATED ALWAYS AS
(TO_DATE(SUBSTR(t.notes, 24, INSTR(t.notes, 'for')-1))) VIRTUAL;
Because virtual columns can't be INSERTed or UPDATEed, doing it this way means that the NOTES field must always contain a valid date string.
Share and enjoy.
Since you are on 11g, and as you say that both columns are in same table. I would suggest, do not add a static column, rather add a virtual column.
So, you need not worry about the insert too. Oracle would give you the computed value without actually storing it.
Just add the virtual column as -
EDIT A closing brace was missing in the end.
ALTER TABLE t ADD
column_name GENERATED ALWAYS AS (to_date(substr(t.notes, 24, INSTR(t.notes, 'for')-1))) VIRTUAL;
NOTE You need to be cautious though. The virtual column would only be able to work if the expression is correctly evaluated. In this case, if the datetime literal is malformed, then the virtual column would fail. Either make sure that all the values in your base column have proper format of datetime literal.
You should do it as two separate operations.
alter table t add cb_time date;
update t set cb_time = to_date(substr(...
You would first add the column with ALTER TABLE and then issue an UPDATE:
ALTER TABLE t
ADD cb_time DATE;
UPDATE t
SET cb_time = SUBSTR(t.notes, 24, INSTR(t.notes, 'for')-1);
But you should also convert the value to a DATE at the same time:
UPDATE t
SET cb_time = TO_DATE(SUBSTR(t.notes, 24, INSTR(t.notes, 'for')-1));
It might even be a good idea to make sure that the value in t.notes contains a date (maybe using regex) so you don't get an error when updating.
You can try;
alter table t add cb_time date;
update t set cb_time = to_date(substr(t.notes, 24, INSTR(t.notes, 'for')-1);

Changing the data type "varchar'' of a column to "DATE" in SQL/ORACLE

I have a table in a database created in oracle 10G. It contains a column of type 'VARCHAR' and stores date as string in this format-> 'dd-mon-yyyy' eg: '12-aug-2008'. Now I want to change the datatype of this column from VARCHAR to DATE. but when i perfrom this query->
ALTER TABLE sales_order
MODIFY COLUMN delivery_date DATE;
I get following error
ORA-00905: missing keyword
I have also tried :
ALTER TABLE sales_order
ALTER COLUMN delivery_date DATE;
I got the error :
ORA-01735: invalid ALTER TABLE option
However when i try to add a fresh column with DATE datatype it works fine.
example :
ALTER TABLE sales_order
ADD delivery DATE;
So, can anybody suggest me a way to change the datatype without deleting the column and its data.
It's the first one, with a slight modification:
ALTER TABLE sales_order MODIFY (delivery_date DATE);
But I'm not sure that will work for those particular datatypes and it also may not work depending on the current data.
You may find it necessary in that case to:
create a new column X of date type.
populate X based on the old column (may need several passes of data fix-ups to work).
delete old column.
rename X to old column name.
Although its a pretty old question, I'll put my solution here for people seeking for a solution:
Here's my solution and it works perfectly.
ALTER TABLE `sales_order` CHANGE `delivery_date` `delivery_date` DATE;
Thank you
modify a column then syntax is:-
alter table table_name modify column_name datatype;
but when you modify the column datatype column must be empty
Thanks for the hints! This got it for me.
alter table Table_Name
Alter column Column_Name datatype
GO
I too was needing to change from a VARCHAR to a date. I am working in SQL 2008 R2. I have found that if I bring in dates as a char or varchar and then change the type to date or datetime, I can catch time/date problems more easily.
THIS STATEMENT WILL FAIL IF YOUR DATA HAS ANY BAD DATES. (I break the date down into sections to find the bad date so I can correct and then I can alter the column type.)
Alternatively, you could create a new column, in which the data type is DATE. then pass the data in your varchar as a date .then drop your initial column and finally rename your new column to what it was initially...code below.
ALTER TABLE my_table ADD (new_col DATE);
UPDATE my_table SET new_col=TO_DATE(old_col,'MM/DD/YYYY');
ALTER TABLE my_table DROP (old_col);
ALTER TABLE my_table RENAME COLUMN new_col TO old_col;
alter table employee add (DOB varchar(10));
if you add a column with datatype varchar and if you want to modify the datatype of DOB then you can use this command ->
alter table employee modify(DOB date);
Now the table is modified.

populate a column with a value based on value of other column alter table sql

I have a table 'Data' with a column 'Date'
I need to add another column called flag and populate it with 0 if the date is less than 2 years from current date and populate it with 1 if the date is more than 2 years from current date.
I did it by adding column using alter table and using update set statement as below
alter table data add flag INTEGER constraint flag_value check (flag in(0,1));
Is there a way to do this using just one alter table statement without using update set?
Is there a way to do this using just one alter table statement without using update set?
Not in sqlite, which does not support computed columns and there is no way to run a default UPDATE via an ALTER TABLE statement.
You could use the view approach from the linked question, or do as you've done and issue separate ALTER TABLE and UPDATE statements.

date syntax issue in sql

I keep getting this error message when trying to change the data type of my column:
alter table x modify column order_date date NOT NULL;
ERROR at line 1
ORA-00905 missing keyword
I not sure where I am going wrong, as I am aware there are many types of dates in sql?
Many thanks
The MODIFY clause does not take COLUMN as a keyword. This will work:
alter table x modify order_date date NOT NULL;
The syntax is documented in the Oracle SQL reference. Find out more.
We only need to include COLUMN with commands which have several different possibilities. For instance, with the ALTER TABLE ... DROP command, because we can drop columns, constraints or partitions....
alter table x drop column order_date ;
"when I tried entering NOT NULL, it said the table needed to be empty"
You should be able to apply a NOT NULL constraint, providing all the rows in the table have a value in the order_date column. The error message you get is quite clear:
ORA-01758 table must be empty to add mandatory (NOT NULL) column
This means your column has some rows without values. So, you need to update the table and populate those rows with some value; what you will use as a default depends on your business rules.