How to populate a timestamp field with current timestamp using Oracle Sql Loader - oracle

I'm reading a pipe delimited file with SQL Loader and want to populate a LAST_UPDATED field in the table I am populating. My Control File looks like this:
LOAD DATA
INFILE SampleFile.dat
REPLACE
INTO TABLE contact
FIELDS TERMINATED BY '|'
OPTIONALLY ENCLOSED BY '"'
(
ID,
FIRST_NAME,
LAST_NAME,
EMAIL,
DEPARTMENT_ID,
LAST_UPDATED SYSTIMESTAMP
)
For the LAST_UPDATED field I've tried SYSTIMESTAMP and CURRENT_TIMESTAMP and neither work. SYSDATE however works fine but doesn't give me the time of day.
I am brand new to SQL Loader so I really know very little about what it is or isn't capable of. Thanks.

Have you tried the following:
CURRENT_TIMESTAMP [ (precision) ]
select current_timestamp(3) from dual;
CURRENT_TIMESTAMP(3)
-----------------------------
10-JUL-04 19.11.12.686 +01:00
To do this in SQLLDR, you will need to use EXPRESSION in the CTL file so that SQLLDR knows to treat the call as SQL.
Replace:
LAST_UPDATED SYSTIMESTAMP
with:
LAST_UPDATED EXPRESSION "current_timestamp(3)"

I accepted RC's answer because ultimately he answered what I was asking but my unfamiliarity with some of Oracle's tools led me to make this more difficult than it needed to be.
I was trying to get SQL*Loader to record a timestamp instead of just a date. When I used SYSDATE, and then did a select on the table it was only listing the the date (05-AUG-09).
Then, I tried RC's method (in the comments) and it worked. However, still, when I did a select on the table I got the same date format. Then it occurred to me it could just be truncating the remainder for display purposes. So then I did a:
select TO_CHAR(LAST_UPDATED,'MMDDYYYY:HH24:MI:SS') from contact;
And it then displayed everything. Then I went back to the control file and changed it back to SYSDATE and ran the same query and sure enough, the HH:MI:SS was there and accurate.
This is all being done in SqlDeveloper. I don't know why it defaults to this behavior. Also what threw me off are the following two statements in sqldeveloper.
SELECT CURRENT_TIMESTAMP FROM DUAL; //returns a full date and time
SELECT SYSDATE FROM DUAL; // returns only a date

If you want to use the table defined default you can use:
ROWDATE EXPRESSION "DEFAULT"

In Sql Developer run:
ALTER SESSION SET NLS_DATE_FORMAT='YYYY-MM-DD HH24:MI:SS'
and then check it with
SELECT SYSDATE FROM DUAL

Related

Oracle- save dates with correct centuryinfo

I am trying to insert nvarchar2 values into my oracle Date field. When my source nvarchar2 value is '20701130', I want to save it as '30/11/2070' in my oracle column. And, if the source value is '21150529', I want to save it as '29/05/2115'.
I tried different formatting options to achieve this, but the year is always getting saved as two digits and is not indicating the correct century.
One of my Queries:
INSERT INTO MY_TABLE (MY_DATE_FIELD)
VALUES(TO_DATE('21061030', 'RRRR-MM-DD'));
Now, when I select after the above query, I get the result as 30/10/06 which is not we want, it should be '30/10/2106'.
You can go about it in many ways:
You alter the session date format itself to a format you want to achieve like :
ALTER SESSION SET NLS_DATE_FORMAT = 'RRRR-MM-DD';
Another approach is once you have achieved a date from a string passed using to_date then you can again add to_char to display the date in a format you want.
To_date will convert your string to a date using the format you have provided but while selecting it will still display the date as per the format provided in the session that's why we use To_char to change it to a format we want to display it into.
select to_char(TO_DATE('21061030', 'RRRR-MM-DD'),'RRRR-MM-DD') "DATE" from dual;
Thanks for the help.
I have pasted my SQL below if anyone is interested.
-- To insert into table
UPDATE MY_TABLE SET MY_DATE_FLD = TO_CHAR(TO_DATE('21061030', 'RRRR-MM-DD'),'DD-MM-YYYY') ;
-- To read from the table, this gave me the result 30/01/2106
select TO_CHAR(DATE_FLD,'DD/MM/YYY') AS START_DATE from MY_TABLE;
Appreciate the help.
Kiran.

Date importing from oracle to hive using spark hive context format in hive should be YYYYMMDD (dt_skey)

I have to import a table from Oracle to Hive using Spark and Scala, date column in Oracle looks like this Oracle column date, I have to cast it to the dt_skey format (YYYYMMDD) in Hive. Table format in Hive is Parquet. How can I do that? I googled it a lot but I didn't find any solution.
Thanks in advance
Assuming your input data is supposed to mean yy-mm-dd (so 16-09-15 means year 2016, month 09, day 15) you probably need a transformation like this:
select to_char( to_date (dt, 'yy-mm-dd'), 'yyyymmdd') from ...
Example:
with my_table ( dt ) as ( select '16-09-15' from dual)
-- this creates a test table my_table with column dt and value as shown
select dt,
to_char( to_date (dt, 'yy-mm-dd'), 'yyyymmdd') as dt_skey
from my_table
;
DT DT_SKEY
-------- --------
16-09-15 20160915
You could also manipulate the input string directly but I would strongly recommend against that. Translating to date and back will catch invalid "dates" in your data before you try to push them to an application. Also, the string manipulation would become complicated if the input strings are inconsistent (for example if something like 16-9-15 is allowed along with 16-09-15).
EDIT: In a comment to his original question, the OP stated that dt is already in DATE format in Oracle. In that case, it should NOT be wrapped within to_date() - that will lead to errors. Rather, the solution is MUCH simpler, all is needed is
select to_char(dt, 'yyyymmdd') from ...

Inserting timestamp into date field(Oracle)

We have tables with date fields(type date) and inserting timestamp into the date field works in some of our oracle environments(We have multiple environments for development) and fails in some environment.
I would like to know why it works in some cases and some cases it fails with ORA-01843: not a valid month
Example
Table1
date1 DATE
The following sql works in some environment and fails in some environments
Insert into Table1 (date1) values ( to_timestamp(sysdate))
Oracle version - Oracle Database 11g
Can we insert timestamp into date field(like above)? Does oracle supports inserting timestamp values to date field?
Thanks in advance.
Instead of sysdate, use current_timestamp (although it is pointless, since it will become a date in the database anyway):
insert into Table1 (date1) values (current_timestamp)
I guess the reason it fails is that to_timestamp needs a varchar and the conversion failed.

Oracle - Timestamp

When importing columns of type "Timestamp" to an Oracle DB:
Import tbl:
02.09.13 00:00:00
After importing:
Oracle tbl:
02.09.13 08:23:44,000000000
In the Oracle tbl the type is also "Timestamp".
How can I remove the ",000000000"?
The TIMESTAMP datatype includes fractional seconds; there is no way to remove them within the database whilst maintaining the datatype. If you don't want fractional seconds then put the date into a DATE column.
It shouldn't matter whether the fractional seconds are stored or not. When selecting from the database into anything other than another date datatype you should format the date as required by the client displaying it. The normal method of doing this would be by using the function TO_CHAR(); for instance:
select to_char(column_name, 'dd.mm.yy hh24:mi:ss') from table_name
You can also do this at a session level by changing your NLS settings.
Unrelated to your question, but to address the comment on a comma being part of your timestamp; the default date format is determined by NLS_TERRITORY. A comma is a perfectly valid character to have here. Altering the NLS_TERRITORY, for example to France, it will appear as part of the NLS_DATE_FORMAT:
SQL> alter session set nls_territory = 'FRANCE';
Session altered.
SQL> select systimestamp from dual
2 ;
SYSTIMESTAMP
------------------------------------------------------------
18/09/13 13:09:54,418387 +01:00

Oracle - How to Convert VARCHAR to DATETIME format

I am using Oracle10g database in which a table contains a Column with Date DataType. I am using the following query to get the record:
select to_char(START_TIME, 'YYMMDD HH24:MI:SS') from table;
So from above query, the result will be of type VARCHAR. I have tried to_Date() method but resulted in displaying only DATE. Can i convert VARCHAR to DATETIME format? The result should be of type DATETIME. Please help me how to resolve this problem.
an Oracle date contains both date and time so you can consider it a datetime (there is no datatype of datetime in Oracle). how is DISPLAYS when you select it is entirely up to your client. the default display setting is controlled by the NLS_DATE_FORMAT parameter. If you're just using the date in your pl/sql block then just assign it into a date datatype and select into that variable without to_char and it will work just fine and contain whatever time component is present in your table.
to control the display, for example using nls_date_format:
SQL> select a from datetest;
A
---------
19-FEB-13
SQL> alter session set nls_date_format='YYMMDD HH24:MI:SS';
Session altered.
SQL> select a from datetest;
A
---------------
130219 07:59:38
but again, this is only for display.
Oracle's Date type fields contain date/time values, therefore converting it to Datetime does not make any sense (it's already datetime)
Read more about oracle date types here
Yeah the Date datatype will meet your needs but you will have to jump through some hoops every time to get the exact time out of it. Definitely use the Timestamp datatype.

Resources