Oracle Default Column Value Calculated Date - oracle

I am trying to create a table that has a default value for a date that is sysdate - 2 in oracle
Oracle seems to be fine with sysdate as the default but not sysdate - 2. Is this possible?

You need to specify the DEFAULT value in brackets:
Create the table:
CREATE TABLE order_status (
order_id NUMBER,
last_modified DATE DEFAULT (SYSDATE - 2)
);
Insert a record to test the default:
INSERT INTO order_status
(order_id)
VALUES
(1);
Select the data from the table to confirm the default worked (Current date 14/11/2011):
SELECT *
FROM order_status;
ORDER_ID LAST_MODIFIED
1 12/11/2011
DB Version 10g.
Hope it helps...

Related

Create ExpiryDate during table definition in Oracle

I'm trying to create a table named MEMBER. The datatype for REGISTERDATE is DATE and the default value is SYSDATE. There is another column named EXPIRYDATE, datatype is DATE and the default value is 1 year after SYSDATE. So how am I going to create for the column of EXPIRYDATE when creating the table definition?
I tried REGISTERDATE DATE DEFAULT SYSDATE,
EXPIRYDATE DATE + 365,
It showed error..
You're so close, you're just missing the default keyword and sysdate after expirydate date
-
create table MEMBER (
REGISTERDATE date default sysdate,
expirydate date default sysdate+365
)
test:
insert into MEMBER(REGISTERDATE) values (sysdate)
output:
Not all years have 365 days; on average, only 3 out of 4. But you can literally define an expression that expresses "1 year after":
create table member (
id number(*,0) not null primary key,
registerdate date default sysdate,
expirydate date default sysdate + interval '1' year
);
Remember that Oracle won't use the default value if you provide your own, even if it's null. You need to complete omit the field from the INSERT INTO statement.
Demo

Default milliseconds to 0 in Oracle

I have a timestamp column in Oracle that has format 'MM/DD/YYYY HH24:MI.SxFF6'.
The data looks like below:
11/09/1917 10:45:28.230000
10/19/2014 18:09:28.410000
12/19/2011 11:06:28.340000
I need the timestamp to retain the value except for getting the milliseconds which need to be defaulted to 000000.
I tried query -
cast(to_char(Local_time, 'MM/DD/YYYY HH24:MI:SS') as timestamp(6))
But it is throwing error - "Not valid month"
Does anyone have any ideas on what I can try to get milliseconds to 0. I use Toad to query the table.
Your TIMESTAMP value does not have any format. All you have is a default display format - defined by current user NLS_TIMESTAMP_FORMAT setting.
Try this one:
CAST(Local_time AS TIMESTAMP(0))
If you like to trunc the milliseconds but haven them still available use
CAST(CAST(Local_time AS TIMESTAMP(0)) AS TIMESTAMP(6))
Something like this, perhaps?
SQL> create table test (col timestamp, result timestamp);
Table created.
SQL> insert into test (col) values (to_timestamp('11/09/1917 15:45:28.230000', 'MM/DD/YYYY HH24:MI:SS.FF6'));
1 row created.
SQL> update test set result = cast(col as date);
1 row updated.
SQL> select * From test;
COL RESULT
------------------------- -------------------------
09.11.17 15:45:28,230000 09.11.17 15:45:28,000000
SQL>

Oracle SQL Developer- How to force 00:00:00 hour when inserting a new DATE value

In my Oracle SQL Developer, i have a table with a column with DATE format. When i insert a new row into this table, and insert a new value in this column, it automatically suggestes me the current date with the current hour.
I would like that it automatically suggestes me current date, but with 00:00:00 hour . Is there some setting or parameter that i can set in my SQL Developer to have this result?
We can't able to insert 00:00:00 hours ... the hour value should be between 1 to 12...
we can use below query to insert 00:00:00 hours but the value will be changed to 12:00:00
INSERT INTO TABLE (DATE_COL) VALUES
( TO_DATE ('11/16/2017 00:00:00 ', 'MM/DD/YYYY HH24:MI:SS '));
It seems to me that your DATE column is set with a DEFAULT of SYSDATE. This means, for any INSERT operations which do not specify a value in your DATE column, the current date and time will populate for that row. However, if INSERT operations do specify a value in your DATE column, then the specified date value will supersede the DEFAULT of SYSDATE.
If an application is controlling INSERT operations on that table, then one solution is to ensure the application utilizes the TRUNC() function to obtain your desired results. For example:
INSERT INTO tbl_target
(
col_date,
col_value
)
VALUES
(
TRUNC(SYSDATE, 'DDD'),
5000
)
;
However, if there are multiple applications or interfaces where users could be inserting new rows into the table, (e.g. using Microsoft Access or users running INSERT statements via SQL Developer) and you can't force all of those interfaces to utilize the TRUNC() function on that column during insertion, then you need to look into other options.
If you can ensure via applications that INSERT operations will not actually reference the DATE, then you can simply ALTER the table so that the DATE column will have a DEFAULT of TRUNC(SYSDATE). A CHECK CONSTRAINT can be added for further integrity:
ALTER TABLE tbl_target
MODIFY
(
col_date DATE DEFAULT TRUNC(SYSDATE, 'DDD') NOT NULL
)
ADD
(
CONSTRAINT tbl_target_CHK_dt CHECK(col_date = TRUNC(col_date, 'DDD'))
)
;
However, if users still have the freedom to specify the DATE when inserting new rows, you will want to use a TRIGGER:
CREATE OR REPLACE TRIGGER tbl_target_biu_row
BEFORE INSERT OR UPDATE OF col_val
ON tbl_target
FOR EACH ROW
BEGIN
:NEW.col_date := TRUNC(SYSDATE, 'DDD');
END tbl_target_biu_row
;
This will take of needing to manage the application code of all external INSERT operations on the table. Keep in mind, the above trigger is also modifying the DATE column if a user updates the specified value column.

How to fetch records according to SYSTIMESTAMP using Hibernate/Oracle11g

I have a field named end_time (of type timestamp(6)) in my Oracle 11g DB. My requirement is to fetch records which are greater than current time stamp.As I work with remote DB, I need the current time of my oracle database server.
After some research I came to know that SYSTIMESTAMP returns current time stamp of machine where DB resides.
So I just put a condition like end_time > SYSTIMESTAMP, but it does not filter records. My end-time is of type timestamp(6).
Do I have to use any conversion function? How can I do it from Hibernate? Any idea?
Can you further explain on "does not filter records", are too many rows in your result or to few?
Your condition looks absolutely ok:
CREATE TABLE mytable (ts TIMESTAMP(6));
INSERT INTO mytable (ts) VALUES (TIMESTAMP '2012-12-06 17:00:00');
INSERT INTO mytable (ts) VALUES (TIMESTAMP '2012-12-06 18:00:00');
SELECT SYSTIMESTAMP FROM DUAL;
06.12.2012 17:10:38.347629000 +01:00
SELECT * FROM mytable WHERE ts > SYSTIMESTAMP;
06.12.2012 18:00:00.000000000
SELECT * FROM mytable WHERE ts < SYSTIMESTAMP;
06.12.2012 17:00:00.000000000

Add DB timestamp to SQL Loader CSV import

I have a client who's CSV file does not contain any dates. They would like a timestamp to indicate as to when each row is loaded into their Oracle 11g database. The CSV file is being supplied by a vendor so I cannot modify the file. I have tried adding a default column value and an "after insert" trigger but with no luck. (Performance is not an issue as this is an off- hours process).
The control files looks like this:
options (skip=1, direct=true, rows=10000)
load data
infile data.dat
badfile sqldatatxtdata.bad
replace
into table LAM.CSV_DATA_TXT
fields terminated by ','
trailing nullcols
(ASSET, ASSET_CLASS, MATURITY, PRICE)
The table looks like such:
create table LAM.CSV_DATA_TXT (
ASSET VARCHAR2(20),
ASSET_CLASS VARCHAR2(50),
MATURITY varchar2(50),
PRICE NUMBER(12,8),
DATE_TIMESTAMP DATE default(SYSTIMESTAMP)
Any other ideas? Thanks.
Adding a TIMESTAMP column with a default value of SYSTIMESTAMP ought to worK:
SQL> create table t23
2 ( id number not null
3 , ts timestamp default systimestamp not null)
4 /
Table created.
SQL> insert into t23 (id) values (9999)
2 /
1 row created.
SQL> select * from t23
2 /
ID TS
---------- -------------------------
9999 25-APR-11 15.21.01.495000
SQL>
So you'll need to explain in greater detail why it doesn't work in your case.
I note that in your example you have created the column as a DATE datatype. This will mean it will truncate the defaulted SYSTIMESTAMP to the nearest second. If you want timestamp values you need to use the TIMESTAMP datatype.

Resources