Import CSV File into Oracle using SQL Developer - oracle

I am trying to import data from a CSV file into a Oracle GroupSpace table using SQL Developer tool. I am getting errors for Date column. My Date column has Date in the below format.
5/6/2016
4/11/2018
11/6/2017...
I get error that the date column has Invalid or Null Date Formats.
Any pointers on what format date format to use when importing Date column would be greatly appreciated.
Thank you so much!
JH

If you aren't sure that dates are valid (for example, nothing prevents you from entering 5/55/2016 into a CSV file, and that certainly isn't a valid DATE value), you can create a staging table whose columns are of VARCHAR2 datatype - it accepts everything, even garbage like 5/55/2016.
Then, after you load data, write some SQL to find errors, fix them, and then move data into the target table.

Check the CSV data in a text editor and look for which part represents the month (the month value will be in the range 1..12). If you are using US dates then use MM/DD/YYYY, otherwise you should probably use DD/MM/YYYY as the date format. If the data has a mixture of both, then you must separate those files and use a different format for each, or you are likely to get invalid date values in your database.

SQL Developer can help you.
You can try the date format masks in the drop-down. If we can guess it, we'll default to one. For some reason your data...fools us, but you can type your own.
If you get something that 'works' the warnings go away.
If you get it wrong, we'll let you know before you even get to the next step.
You can find all the data format masks here.

Related

Import CSV to SQL - with datetime/timestamp

I'm currently working on importing a CSV(with thousands of rows) into SQL.
So far, I have created a table in SQL and populated it with column names, as well as data types corresponding to the column names(all of these correspond to the columns in the csv)
In this csv file, one of the column gives a timestamp in the form of:
2/3/2019 12:00:00 AM (MM/DD/YY HH:MM:SS)
The corresponding column I created in sql has a datatyoe if timestamp(6)
My next step is to right click on the table I just created in SQL and hit "Import Data" I then go through the steps of importing the csv to sql, however, the timestamps are not correctly imported. I know this because all of the timestamps in the csv are from February 3, 2019, and in SQL it ranges from
20-MAR-02 7:00:05 PM(DD-MON-YY HH:MM:SS) to 20-APR-02 7:00:00 PM(DD-MON-YY HH:MM:SS)
I don't know a way to get the format to match up from the csv(MM/DD/YY HH:MM:SS) to SQL(DD-MON-YY HH:MM:SS)
Any help is appreciated! Thank you!!
Edit: #Sean Lange: Thanks! When I try to use datetime(6)as a data type I get an error that says 00907:missing right parenthesis? If I try just datetime I get an error 00902: invalid datatype? Any thoughts?
Edit#2: Yes, I am using oracle, sorry the language on these websites confuses me sometimes because I'm still learning. Any help would still be wonderful, going through step by step specifically
Edit #3: Thank you for your help. Sorry once again for my lack of knowledge, I'm using oracle sql developer.
After creating the table incorrectly, I then typed these lines of code:
alter table table_name
alter column column_name to_timestamp(yourFieldName, 'mm/dd/yyyy hh:mi:ss am');
However, I was given this error.
Error report -
ORA-01735: invalid ALTER TABLE option
01735. 00000 - "invalid ALTER TABLE option"
I truly do appreciate all of your help, thank you all again!
I also tried to update the forenter image description heremat in the wizard and was given an error. Attached are screenshots explaining what I did
enter image description here
I dont think the pictures are working, but the error said that the date format wasnt recognized.
You haven't specified what client you're using to connect to Oracle Database - TOAD? Oracle SQL Developer? Something else? ...So I can't give you specific step-by-step instructions.
However, your basic problem is that you need to convert from a text string to a timestamp, and for that you need to enter your date format string. Somewhere your "Import Data" wizard should have a step for either (a) entering an expression or (b) entering a date format.
The date format string you want is 'mm/dd/yyyy hh:mi:ss am' (not case sensitive, you can use caps if you prefer, or pm instead of am).
The expression would be
to_timestamp(yourFieldName, 'mm/dd/yyyy hh:mi:ss am')

Oracle SQLLDR - Load Record with Invalid Date, Replace Invalid Date with Null

There are records in my source text file with invalid date values. The invalid date values are inconsistent in format due to manual entry. I still want to load all of these records, but I want to replace the invalid date value with a null.
Please let me know if/how this is possible via SQLLDR control file commands. I want to avoid creating any custom functions. Something simple that generally refers to errors/exceptions and that works (unlike the below) is ideal:
DATE "MM/DD/YYYY" NULLIF (FROM_DOS=EXCEPTION)
Thanks!
As far as I can tell, that won't go in a single pass. I'd suggest you to try a relatively simple approach:
load the original data "as is"
rows with invalid dates won't be loaded, but will end in the .BAD file
then modify the control file:
source will now be the .BAD file
load NULL into the date column (FILLER might help)
Alternatively, you might use the source file as an external table and write (PL/)SQL against it to load data into the target table. It allows you to actually code whatever you want, but - as you said you don't want to create a custom function (which would decide whether the input data is - or is not - a valid DATE value), I presume you'd rather skip that option.

How to Insert a Timestamp in Oracle in a Specific Format

I am at a loss as how to insert the current time in a different format than the default. Can somebody help explain?
Here is how my table was created:
CREATE TABLE ACTIVITY_LOG
(
TIME TIMESTAMP NOT NULL
, ACTIVITY VARCHAR2(200) NOT NULL
);
My insert command works:
insert into activity_log
values (localtimestamp,'blah');
But how do i insert the localtimestamp value into my table in a different format using the various MM DD YY HH MM SS tags? I've tried the following, but it gives me the ORA-1830: date format picture ends before converting entire input string error.
insert into activity_log
values (to_timestamp(localtimestamp,'YYYY/MM/DD'),'blah');
You don't insert a timestamp in a particular format. Timestamps (and dates) are stored in the database using an internal representation, which is betwen 7 and 11 bytes depending on the type and precision. There is more about that in this question, among others.
Your client or application decides how to display the value in a human-readable string form.
When you do:
to_timestamp(localtimestamp,'YYYY/MM/DD')
you are implicitly converting the localtimestamp to a string, using your session's NLS settings, and then converting it back to a timestamp. That may incidentally change the value - losing precision - but won't change how the value is stored internally. In your case the mismatch between the NLS setting and the format you are supplying is leading to an ORA-01830 error.
So your first insert is correct (assuming you really want the session time, not the server time). If you want to see the stored values in a particular format then either change your client session's NLS settings, or preferably format it explicitly when you query it, e.g.:
select to_char(time, 'YYYY-MM-DD HH24:MI:SS.FF3') from activity_log
You don't seem to provide any indication of what your 'localtimestamp' is - is that pseudocode? A variable name? A column you haven't shown the definition for?
What data type is 'localtimestamp'? What data does it contain? Pertinent questions as other answers point out, because if it truly is a time stamp then oracle will be converting it to a string for you, before passing that string to to_timestamp() in your final query. Your initial stab at it should just work if the variable is a timestamp, containing a timestamp
Ultimately "date format picture ends" means "you passed me a string looking like '2017-05-17 12:45:59', but claimed it was only 'yyyy-mm-dd'. What was I expected to do with the rest of it?"
Your current final comment on your question "I was hoping to look in the table and see a useful looking time" - that's your query tool's problem. Have a look in the setting of your query tool and change the date format it displays. As has been noted, dates in oracle are stored as a decimal number days since a certain moment in time. If 0 represents 01 Jan 1970, then 1.75 represents 6pm on the 2 Jan 1970. It is up to the end program the user is using, to format the date into something you like.. you cannot "insert a timestamp with a different format" because time stamps don't have a format any more than a number like 1.75 has a format. It is what your query does with it when it gets it out, that gives it the format:
To_char(timestampcol, 'yyyy mm did')
To-char(tomestampcol, 'mon dd yyyy')
These use oracles built in date formatter, that turns that decimal number of the date into a string in the given format; you will see a string.. or you can just write "select * from table" and run it in TOAD and toad will show you the dates according to the format in settings, or you can write a c# program and get a load of date objects out and call my date.ToString("yyyy-MM-dd") on them to format them. The idea I'm trying to get across is that you don't pick the date format on the way in, you pick it on the way out, if you don't like what you're looking at, you have to change it on the way out, not the way in

wrong date format in bi reports oracle

I have the following date which is in varchar2(11) column in database:
select valid_untill from SALES_ORDERS_V where header_id = 7999410;
30-May-2016
Using rtf template and xml source, the report output (PDF) is:
4950-11-19 04:45:49:0
I don't know its equal to "30-May-2016".
Why this is showing this, as I did not do any formating in rtf?
Not familiar with either RTF or XML-Publisher, but whenever you retrieve a date saved in string format, IF you use it as a date in your code and not as a string, you must make sure you retrieve it correctly.
In this case, with your select statement: it shouldn't be select valid-until from... (or is it really misspelled, with two l at the end: valid_until?) If it is meant to be used as a date, it should be
select to_date(valid_until, 'dd-Mon-yyyy') from ...
Really the problem here is that the date is stored as a string and not in the date datatype. Good luck!

Oracle sqlldr timestamp format headache

I'm struggling to get sqlldr to import a csv data file into my table, specifically with the field that is a timestamp.
The data in my csv file is in this format:
16-NOV-09 01.57.48.001000 PM
I've tried all manner of combinations in my control file and am going around in circles. I can't find anything online - not even the Oracle reference page that details what all the date/timestamp format strings are.
Does anyone know where this reference page is, or what format string I should be using in my control file for this timestamp format.
For reference, this is what I've most recently tried:
load data
infile 'kev.csv'
into table page_hits
fields terminated by "~"
( ...
event_timestamp TIMESTAMP "dd-mmm-yy hh24.mi.ss",
...)
you can try this format:
event_timestamp TIMESTAMP "dd-MON-yy hh.mi.ss.ff6 PM"
You can browse all available formats in the SQL reference documentation.

Resources