oracle db column date default value not sysdate - sql

how to set the default value for a date column for something diferent that sysdate? like 01/07/1998, im currently using
ALTER TABLE XILIADO
ADD (FECHA_AFIL DATE DEFAULT sysdate NOT NULL);

Just use a fixed date, either with to_date() with a string literal and matching format model, or a date literal:
ALTER TABLE XILIADO
ADD (FECHA_AFIL DATE DEFAULT DATE '1998-07-01' NOT NULL);
(assuming your example was in DD/MM/YYYY format; date literals are unambiguous, which is another advantage of using them.)

For any fixed date, use to_date(date_value, date_mask) along with alter column.
ALTER TABLE XILIADO
ADD FECHA_AFIL DATE DEFAULT TO_DATE('01-07-1998','DD-MM-YYYY') NOT NULL

Related

Converting all data in a Varchar column to a date format

I'm working on a table with a column, 'Expiry Date', as a varchar with all data formatted as DD/MM/YYYY.
The creator of the table has used the wrong type for this expiry date column and now the client needs to filter and show all records before and after the current date as the time. This means the type needs to be changed to date or datetime type to be able to use the CURDATE() function.
However, the current format of the values does not satisfy and wont allow the type to change unless the format is changed to YYYY-MM-DD (or similar).
Is there any way to mass format the values in this column and this column alone as there are thousands of entries and formatting one by one would be extremely time consuming.
Let me assume that you are using MySQL.
Perhaps the simplest method is to add a generated column that is a date:
alter table t add column expiry_date_date as
(str_to_date(expiry_date, '%d/%m/%Y'));
You can also fix the data:
update t
set expiry_date = str_to_date(expiry_date, '%d/%m/%Y');
This will implicitly convert the result of str_to_date() to a date, which will be in the YYYY-MM-DD format.
More importantly, you can then do:
alter table t modify column expiry_date date;
Here is a db<>fiddle.
You can do similar operations in other databases, but the exact code is a bit different.
What you need is an update on that column, but before doing it I suggest you to check if the result is what you want.
select replace(expiry_date, '/', '-') new_expiry_date
from table_name
If this returns the results you want you can run the following update:
update table_name
set expiry_date = replace(expiry_date, '/', '-')
Of course you will need to replace expiry_date and table_name with the names of your column and table.

can we insert date into varchar2 in oracle without converting by to_char?

can we insert date into varchar2 in oracle without converting by to_char?
Oracle will convert the date into a string using what localization settings are in place during the insert. This is true not only of Oracle but of any database. For a date, that might commonly be in the format "DD-MON-YY".
Here is an example of inserting a date into a varchar2 column.
That said, you should not do this. You should be storing date/time values in the database using the correct data type -- and that would be either date or timestamp. If you want to insert a constant, then use the date or timestamp qualifier with the appropriate value following it.

QMF query about date format

I have one column with the date format of yyyy-mm-dd and another with the format of mm/dd/yyyy. How can I change the second format to look like the first in QMF sql writing?
You can alter your table columns using an SQL command like this. I have specifically changed the column to a date format in the example
ALTER TABLE 'Schema_Name'.'Table_Name'
CHANGE COLUMN 'Column_Name' 'New_Column_Name' DATE NULL DEFAULT NULL;

SQL oracle beginner questions

Question1:
Do i have to use to_date while inserting date?
INSERT INTO some_table (date1, date2)
VALUES (to_date('2012-10-24','YYYY-MM-DD'), to_date('2012-10-24','YYYY-MM-DD'));
Or can just insert as string? Will everything be OK this way too? I've tried and it worked.
INSERT INTO some_table (date1, date2)
VALUES ('2012-10-24',2012-10-24');
Question2:
What happens if i won't name columns that i'm inserting into? It works, but my question is if it inserts randomly now or it takes order of columns during creation of table?
INSERT INTO some_table
VALUES ('2012-10-24',2012-10-24');
1 seems to only work with the 'YYYY-MM-DD' format:
http://docs.oracle.com/cd/B28359_01/server.111/b28286/sql_elements003.htm#SQLRF51049 says
You can specify a DATE value as a string literal ... to specify a DATE value as a literal, you must use the Gregorian calendar. You can specify an ANSI literal... The ANSI date literal contains no time portion, and must be specified in the format 'YYYY-MM-DD'.
However, it might work with time if you use the
Alternatively you can specify an Oracle date value... The default date format for an Oracle DATE value is specified by the initialization parameter NLS_DATE_FORMAT.
For question 2, it uses the order at definition of the table. However you have to give values for all columns in that case.
Oracle supports Standard SQL date literals (since 9i).
It's DATE followed by a string with 'yyyy-mm-dd' format
DATE '2014-05-10'
It's much shorter than TO_DATE and it's independent of any NLS settings.
Similar for timestamps:
TIMESTAMP '2014-05-10 09:52:35'
Regarding your 2nd question: It's the order of columns as defined within the CREATE TABLE.
You could even do it like this one:
ALTER SESSION SET NLS_DATE_FORMAT = 'MM:YYYY:DD';
INSERT INTO some_table (date1) VALUES ('05:2014:10');
...but doing it like this is not recommended. Use TO_DATE or DATE Literal, e.g. DATE '2014-05-10' instead. It makes your life easier.

SQL Server default date time stamp?

I have a field that when something is inserted I want it to get the current Date & Time and insert this into the database. Is there a way to get the date & time, and set it as the default value?
Currently the default value is: (getdate()) Which sets only the date. How do I also set the time?
GETDATE() is a date and time in SQL Server.
Run SELECT GETDATE() to verify this.
What is the datatype of your field? If it's DATE then it will not hold time values as well.
One easy way is to give the field a default constraint:
create table YourTable
(
... other columns ...
CreateDt datetime default getdate(),
... other columns ...
)
A disadvantage of this method is that you can overwrite the value by specifying it in an insert clause.
Personally I would like to use GETUTCDATE() instead GETDATE() to avoid confusions.
SYSDATETIME() will get the current date and time.
Make sure the data type of the column is datetime, and not just date or it won't be able to hold a datetime.
Most SQL implementations (engines) do have CURRENT_TIMESTAMP function.