Handle carriage return in the ELF file while load the data through SQLLDR in Oracle DB - plsql

Some times the ELF file which we received may contain carriage return or line feed char at the end of the segment/field. In this case, the data is staged properly since the data type of most of the stage table columns is VARCHAR2.
When move the data from stage table to transaction table, it is throwing data conversion error when the numeric or decimal value in the stage table holds carriage return or line feed char value.
Each segment in the file should end with the delimiter(|) but if delimiter not added at the end of the segment then we are facing the issue while move the data from stage to transaction table.
In the above data screen shot, 1st line will loaded properly but 2nd and 3rd (line's/) segment's last field value will be inserted in the table with carriage return char.
Below is the table definition:
OPTIONS (ROWS=1000, READSIZE=2097152, BINDSIZE=2097152, PARALLEL=TRUE)
LOAD DATA
INFILE 'ELFFilePath'
APPEND
INTO TABLE STAGE_TABLE1
WHEN
FEED='FEED'
FIELDS TERMINATED BY '|'
TRAILING NULLCOLS
(
ELEMENT_NUMBER POSITION(1) DECIMAL EXTERNAL,
COLUMN1 CHAR(5),
COLUMN2 CHAR(11),
COLUMN3 CHAR(10),
)
INTO TABLE STAGE_TABLE2
WHEN
DOMAIN='DOMAINNAME'
FIELDS TERMINATED BY '|'
TRAILING NULLCOLS
(
ELEMENT_NUMBER POSITION(1) DECIMAL EXTERNAL,
COLUMN1 CHAR(5),
COLUMN2 CHAR(11),
COLUMN3 DECIMAL EXTERNAL,
COLUMN4 NUMBER
)
INTO TABLE STAGE_TABLE3
WHEN
DOMAIN='DOMAINNAME2'
FIELDS TERMINATED BY '|'
TRAILING NULLCOLS
(
ELEMENT_NUMBER POSITION(1) DECIMAL EXTERNAL,
COLUMN1 CHAR(5),
COLUMN2 CHAR(11),
COLUMN3 CHAR(30),
COLUMN4 CHAR(30),
COLUMN5 CHAR(50),
COLUMN6 CHAR(15)
)
Please help me to handle carriage return/Line feed while load the data through SQLLDR

How about removing CRLF? Something like this presuming COLUMN3 is the last column to be loaded; chr(13) is carriage return, chr(10) is line feed character:
INTO TABLE STAGE_TABLE1
WHEN
FEED='FEED'
FIELDS TERMINATED BY '|'
TRAILING NULLCOLS
(
ELEMENT_NUMBER POSITION(1) DECIMAL EXTERNAL,
COLUMN1 CHAR(5),
COLUMN2 CHAR(11),
COLUMN3 CHAR(10) "replace(:column3, chr(13) || chr(10), '')"
)

SQL*Loader assumes the file line ending based on the OS it's running on. You can override that; one way is to change
INFILE 'ELFFilePath'
to
INFILE 'ELFFilePath' "str '\r\n'"

Related

json-extract sqlite format

I used the following command
SELECT json_extract(data,'$.address') FROM data;
and output as CSV file.
Output in CSV file is
enter image description here
Field (column) in CSV file is saved as 2 lines for 1 field (column).
Eg-
"71 CHOA CHU KANG LOOP
NORTHVALE"
How could I save field(column) as 1 line ?
That is I don't want to include new line character in filed(column).
Eg-
"71 CHOA CHU KANG LOOP NORTHVALE"
Thanks.
Just replace the new line character:
select replace(json_extract(data,'$.address'), char(10), '') from data;
This will catch the newline character ('\n'). If you want '\r' and '\r\n' too:
select replace(
replace(json_extract(data,'$.address'), char(10), ''),
char(13),
''
) from data;

update blob column with base64 value PL/SQL

everyone.
I want to update a blob column with a base64 value generated from a PDF file without any directory in a server, but I have a ORA-01704 error: string literal too long (Image):
ORA-01704 error updating blob column
Is there a short way to do that with PL/SQL?
LOAD DATA
INFILE example13.dat
INTO TABLE EXAMPLE13
FIELDS TERMINATED BY ','
( EMPNO INTEGER EXTERNAL,
ENAME CHAR,
JOB CHAR,
MGR INTEGER EXTERNAL,
SAL DECIMAL EXTERNAL,
COMM DECIMAL EXTERNAL,
DEPTNO INTEGER EXTERNAL,
RES_FILE FILLER CHAR(60),
"IMAGE" BFILE(CONSTANT "ORDIMGDIR", RES_FILE)
)

how to trim trailing spaces in teradata table columns

i want to trim trailing spaces for teradata table columns,
i do it like this,
trim(trailing from dictionary_managed_databases.dbname),
or use trim directly,
trim(dictionary_managed_databases.dbname),
but the result shows:
seems the trim do not work,
not sure how to do it in teradata,
create volatile table test ( dbname varchar(128) CHARACTER SET UNICODE ) on commit preserve rows;
insert into test values ( 'Database-Name' );
-- you don't need to trim a varchar column
select dbname || '~' from test;
(dbname||'~')
---------------------------------------------------------------------------------------------------------------------------------
Database-Name~
-- it is always max length, so not to loose any possible content
select trim(dbname) || '~' from test;
(Trim(BOTH FROM dbname)||'~')
---------------------------------------------------------------------------------------------------------------------------------
Database-Name~
-- you may cast it to shorten the resulting column
select cast(trim(dbname) as varchar(30)) from test;
Trim(BOTH FROM dbname)
------------------------------
Database-Name
-- it will never be less then the header, even if the content is less
select cast(trim(dbname) as varchar(10)) from test;
Trim(BOTH FROM dbname)
----------------------
Database-N
-- but it will truncate the result
select cast(trim(dbname) as varchar(10)) as dbname from test;
dbname
----------
Database-N
sel
dictionary_object_map.moId,
trim(dictionary_managed_databases.dbname)|| '~',
dictionary_deployed_info.dictionaryId,
dictionary_deployed_info.dictionaryName,
dictionary_managed_objects.moname
from dictionary_object_map,
dictionary_deployed_info,
dictionary_managed_objects ,
dictionary_managed_databases
where
dictionary_object_map.dictionaryId=dictionary_deployed_info.dictionaryId
and dictionary_object_map.moid=dictionary_managed_objects.moid
and dictionary_managed_databases.moDBId=dictionary_managed_objects.moDBId
and dictionary_managed_databases.dbname = 'customerservice';
the result
don't understand why output of field dbname still look like this,

fetching values with apostrophe on PLSQL

I want to get the value like this:
select column1, column2 from blah:
COLUMN1 COLUMN2
-----------------
value1 value2
I want to fetch them with apostrophe
COLUMN1 COLUMN2
-----------------
'value1' 'value2'
I need to add apostrophes to the column name but I didn't know how to do that in PLSQL
select '''+column1+'' from blah
Any suggestions?
Here is another way using the CHR() function to return the single-quote character given it's ASCII decimal equivalent. Simplifies the escaping of single quotes.
select chr(39) || 'testing' || chr(39)
from dual;
In a PL/SQL program, you could even define it as a CONSTANT, then use it by that name in your code:
declare
SQ CONSTANT VARCHAR2(1) := CHR(39); -- Single quote
buffer VARCHAR2(20);
begin
select SQ || sysdate || SQ
into buffer
from dual;
dbms_output.put_line(buffer);
end;
I feel it's cleaner than 4 quotes in a row and likely easier to maintain if you have lots of columns in a single select you have to do that with.
Please try the || operator:
select '''' || column1 || '''' from blah
Also the CONCAT function can be used

How to update all columns that are NULL to empty string?

I want to update my table so that every column that has a value of NULL is updated to be and empty string.
Currently I have the following query, but it would only update one column and I want to update all columns that are NULL to empty string.
UPDATE table SET column1='' WHERE column1 IS NULL
You can update multiple columns with one statement by doing something like this:
UPDATE table SET column1='', column2='', column3='' WHERE column1 IS NULL
HOWEVER thsi will only update based on the where clause.
For what you are trying to do, you'll need separate statements.
UPDATE table SET column1='' WHERE column1 IS NULL
UPDATE table SET column2='' WHERE column2 IS NULL
UPDATE table SET column3='' WHERE column3 IS NULL
EDIT Try this:
UPDATE table SET column1= IfNull(column1,''), column2= IfNull(column2,'') , column3= IfNull(column3,'')
You can update a column to itself and check for null there...
UPDATE table SET
column1 = ISNULL(column1,''),
column2 = ISNULL(column2,''),
column3 = ISNULL(column3,'')
etc..
No WHERE clause needed because you want it to run on all records.
Actually you can do something like this
DECLARE #sql varchar(max)=''
select #sql= #sql+''+ c.name + '= CASE WHEN ' +c.name+'=''''THEN NULL ELSE ' +c.name+' end,
'
from sys.tables t
JOIN sys.columns c
ON t.object_id = c.object_id
WHERE t.object_id = 1045578763 -- Your object_id table
PRINT 'UPDATE <TABLE>
SET '+#sql
You can't dynamically create a statement in standard SQL. Without outside help of a programming language, you'll need to repeat this statement for every column.
You need to specify the columns that you want to change. So if your table had four columns, you need multiple queries like this:
UPDATE table SET column1 = '' WHERE column1 IS NULL;
UPDATE table SET column2 = '' WHERE column2 IS NULL;
UPDATE table SET column3 = '' WHERE column3 IS NULL;
UPDATE table SET column4 = '' WHERE column4 IS NULL;
It gets a little easier if you just want to set all columns to an empty string provided that just one of the columns is NULL, but this may not be what you want:
UPDATE table SET column1 = '', column2 = '', column3 = '', column4 = ''
WHERE (column1 IS NULL
OR column2 IS NULL
OR column3 IS NULL
OR column4 IS NULL);
This seems impossible, sadly.
For those who wonder why this actually needs to be done, you haven't dealt with linking filemaker to mysql with ODBC.
Long story short, FM "helpfully" coerces all empty strings to null before sending them on to mysql, and applies a non empty constraint when a mysql column has a not null constraint.
This behavior is actually correct for non string data, since empty mean zero for that. But it causes issues for string data.
Since NEW columns keep getting added to the table, it would be useful to be able to update all columns that can take empty strings to have them instead of null without having to know what they all are in advance.

Resources